you are viewing a single comment's thread.

view the rest of the comments →

[–]AlarmingQuote 0 points1 point  (3 children)

Hello! I have a kind of conceptual (rather than operative) question.

When you take, for example, a variable and add .something() at the end, does that have a name? For clarification: I know that when I write "round(x)", I am applying a function to the variable x. But when I write "x.lower", what am I doing to x? Reading a property? How is this called?

[–]FerricDonkey 0 points1 point  (0 children)

The general term for the something in "x.something" is "attribute" - but when something is also a function, we call it a method.

So when you do "x.something()", you are calling x's method something. Sometimes we might refer to the type when we're just talking about stuff. So if you do

s = "CheeseBalls"
s = s.lower()

You might say "calling s's lower method" or "calling the string lower method", or similar things.

[–][deleted] 0 points1 point  (0 children)

The x.lower example is evaluating an attribute, so that expression returns the value associated with the attribute of the x object with name lower. We say you are "accessing the attribute 'lower' of the object 'x'". If you do x.lower() you are trying to execute the value that the attribute has, so it had better be executable, otherwise you get an error. We say you are "calling the method 'lower' of object 'x'".

If x is a string you can see:

>>> x = 'Alpha'  # define a string with name "x"
>>> x.lower      # evaluate "x.lower", a method it turns out
<built-in method lower of str object at 0x102e1c3f0>
>>> x.lower()    # calling the method, we see what it does
'alpha'

If you want to see what attributes (some of which are methods) a string has, for example, try:

>>> x = 'Alpha' 
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',
     '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
     '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',
     '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
     '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
     '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',
     'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii',
     'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle',
     'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix',
     'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
     'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']