In Python, each object has one namespace -- the object's "attributes", accessible in that namespace, can be methods, simple references to other objects (callable or not), or be synthesized on the fly by descriptors (plain attributes may live in the instance or in the class, but descriptors -- including functions as descriptors that synthesize methods -- are only used if they live in the class, not the instance; so in particular "special methods" are only special if defined in the class, not the instance). Attribute-handling builtins and special methods (getattr
, setattr
, __getattr__
, __setattr__
, ...) work just the same way across the single namespace of the object, whether referring to "plain attributes" or not.
The key point is that for any object a
, in Python, a.b
may be a method (or other callable) or not: the Python compiler doesn't know (nor care), you can take the reference a.b
and (e.g.) pass it as an argument, return it as a result, append it to a list, and so forth, none of these operations implies calling a.b
. If and when you want to (try and) call a.b
, you do it explicitly by postfixing parentheses: a.b()
if you want to call it without arguments.
In Ruby, methods and attributes of an object live in separate namespaces (so you can have an object with a method and an attribute with the same name, which in Python you can't) and "just mentioning" an argument-less method implicitly calls it (so c=a.b
might be just taking an attribute reference, or it might be calling a.b()
; if b
names both a method and an attribute in a
, I don't recall what heuristic rule is used to disambiguate the use). So if you want to just take method references (e.g to stash in some container or use as arguments or return values), and later perform the call, you use different syntax.
Smalltalk also has separate namespaces, like Ruby, but then you can't refer to a given object's "non-method" attributes (each object only "sees" its own attributes), so this ambiguity does not arise (but you still do have to use specific messages to extract, and later call, a "method reference").