Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I read in a book that both methods and fields are considered the attributes of a class in Python. However, recently I was told by a friend of mine that methods may not be considered the attributes of a class.

Then I decided to check this out again, and found this question mentioned on Wikipedia saying that this is a Python "tradition" to refer to methods as attributes, in contrast to other object-oriented programming languages.

So my question is: why are methods considered the class atributes along with the fields in Python in contrast to other languages?

Thank you!

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Your friend was wrong. Methods are attributes.

Everything in Python is objects, really, with methods and functions and anything with a __call__() method being callable objects. They are all objects that respond to the () call expression syntax.

Attributes then, are objects found by attribute lookup on other objects. It doesn't matter to the attribute lookup mechanism that what is being looked up is a callable object or not.

You can observe this behaviour by looking up just the method, and not calling it:

>>> class Foo(object):
...     def bar(self):
...         pass
... 
>>> f = Foo()
>>> f.bar
<bound method Foo.bar of <__main__.Foo object at 0x1023f5590>>

Here f.bar is an attribute lookup expression, the result is a method object.

Of course, your friend me be a little bit right too; what is really happening is that methods are special objects that wrap functions, keeping references to the underlying instance and original function, and are created on the fly by accessing the function as an attribute. But that may be complicating matters a little when you first start out trying to understand Python objects. If you are interested in how all that works, read the Python Descriptor HOWTO to see how attributes with special methods are treated differently on certain types of attribute access.

This all works because Python is a dynamic language; no type declarations are required. As far as the language is concerned, it doesn't matter if Foo.bar is a string, a dictionary, a generator, or a method. This is in contrast to compiled languages, where data is very separate from methods, and each field on an instance needs to be pinned down to a specific type beforehand. Methods generally are not objects, thus a separate concept from data.

share|improve this answer
    
That's a clear explanation. Thanks! But what about the other languages (like C++, for example)? Are there any difference between the methods and fields for the attribute lookup process in those languages? –  v_2e Aug 2 '13 at 8:52
    
Python is a dynamic language, and attributes do not need to be typed beforehand. With C++ on the other hand, the compiler nails everything down, so it makes much more sense to distinguish between data and methods. Methods are not objects in C++. –  Martijn Pieters Aug 2 '13 at 8:56
    
In Python 3 methods are even less special. There aren't any unbound methods any more. All functions are descriptors which create bound methods when looked up through the class (and as in Python 2, any custom callable class can add a three-line __get__ to get the same behavior, though I've never seen anyone do this). –  delnan Aug 2 '13 at 10:15
    
Yup, just didn't want to go into that much excruciating detail.. –  Martijn Pieters Aug 2 '13 at 10:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.