2

I was wondering, what happens if a method and a attribute has the same name in a class. Will the program first search for the attribute in a object and if a method in a class has the same name, will the program skip it and just focus on the object?

3
  • It will be the one that is defined the latest Commented Feb 7, 2013 at 9:23
  • An easy way to answer this question would be to write such a class and see what happens. Commented Feb 7, 2013 at 9:24
  • methods are just objects like strings, ints, lists, etc. What you think of as a "method" is just an object that is referred to by an attribute. Commented Feb 7, 2013 at 12:57

2 Answers 2

1

It depends on which one is assigned first.

If the method is assigned first, the attribute will be the one you will access,

e.g.

class Example(object):
    def __init__(self):
        self.x = 44

    def x(self):
        return "hi"

print Example().x

Methods are "assigned" when the class is processed, where as x is assigned at initialization. This means that x overwrites the method.

This is of course different for class level attributes because they are assigned at the same time as the method

class Example(object):
    x = 43

    def x(self):
        return "hi"

print Example().x

Shows that the method overwrites the attribute.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! But You said that x is assigned at initialization, and that means that the method is assigned first, so shouldnt print Example().x return hi? because it was defined the latest?
The important key to understanding this is that functions/methods in Python are themselves objects. Creating a method is fundamentally assigning a value to an attribute; it just happens to be a value that is callable. Once you have this understanding, it is straightforward to see that the assignment that happens last "wins."
"it is straightforward to see that the assignment that happens last "wins."" I understand what you mean but the assignment that happend last in the example above, the first one was the def x(self)... which return hi. And thats why it should print out hi. because it was the latest definition.
The order of the assignment doesn't matter: you could have a class with an x attribute assigned then assign Example.x = <some function> and you've assigned the method second but the instance attribute still takes precedence. Also as I explain in my answer the object in the class will take precedence if it is a descriptor.
1

Jakob's answer is mostly accurate but incomplete. If the x defined in the class follows the descriptor protocol and has __get__ and __set__ methods defined then it will continue to take precedence over any 'x' in the instance dictionary. The simplest way to see this is to use a property:

>>> class Example(object):
    def __init__(self):
        self.x = 44

    @property
    def x(self):
        return "hi"
    @x.setter
    def x(self, value):
        self.__dict__['x'] = value


>>> Example().x
'hi'
>>> Example().__dict__['x']
44

So the actual rule is that Python looks in the class (and its bases), and if it finds a matching object and the object has a __get__ method (or __set__ if setting) then it calls the __get__ method otherwise it looks for an instance attribute and uses that if there is one or if not falls back to using the class attribute (i.e. the method).

The lookup may be further complicated if either __getattribute__ or __getattr__ is defined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.