0

Why in the methods len( ), the placement of variables different from the others, for example theupper( )

# with len()
len(variable_name)

# with upper()
variable_name.upper()

Things may be trivial , it makes me uncomfortable if do not know it ... thanks.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • `len` is a Python ["built-in *function*"](https://docs.python.org/3/library/functions.html) that is a general operation that works for sequences; strings can be thought of a sequence of characters. There are only a few such built-in functions, selected largely due to their general usefulness. `str.upper` is a string-specific *method*; as is [`str.__len__` which implements the "\_\_len\_\_ protocol" required for `len`](http://stackoverflow.com/questions/237128/is-there-a-reason-python-strings-dont-have-a-string-length-method). – user2864740 May 31 '16 at 04:59

1 Answers1

0

variable_name.upper() is calling a method of the variable_name class or an inherited class.

len(variable_name) is calling Python's built-in len function on a variable_name object for which the len function determines the length depending on the type of object, be it a str or list for example.

So the placement of the variable_name tells you if you are calling a class attribute (variable_name in front) OR you are passing a variable into a method which is not an attribute of the variable_name class (variable_name in the brackets).

EngineerCamp
  • 661
  • 1
  • 6
  • 16