AttributeError: 'property' object has no attribute
Solution 1
In addition to needing to inherit from object
, properties only work on instances.
a = A()
a.db.doSomething("blah")
To make a property work on the class, you can define a metaclass. (A class is an instance of a metaclass, so properties defined on the metaclass work on the class, just as properties defined on a class work on an instance of that class.)
Solution 2
You aren't using classes correctly. A class is (normally) two things:
- A factory for creating a family of related objects
- A definition of the common behaviour of those objects
These related objects are the instances of the class. Normal methods are invoked on instances of the class, not on the class itself. If you want methods that can be invoked from the class, without an instance, you need to label the methods with @classmethod
(or @staticmethod
).
However I don't actually know whether properties work when retrieved from a class object. I can't check right now, but I don't think so. The error you are getting is that A.db
is retrieving the property object which defines the property itself, it isn't "evaluating" the property to get A.__db
. Property objects have no doSomething
attribute. Properties are designed to be created in classes as descriptions of how the instances of those classes work.
If you did intend to be working with an instance of A, then you'll need to create one:
my_a = A()
my_a.db.doSomething("blah")
However, this will also fail. You have not correctly written getDB
as any kind of method. Normal methods need an argument to represent the instance it was invoked on (traditionally called self
):
def getDB(self):
...
Static methods don't, but need a decorator to label them as static:
@staticmethod
def getDB():
...
Class methods need both an argument to receive the class they were invoked on, and a decorator:
@classmethod
def getDB(cls):
...
Ben
Experienced with C, C#, Java, Python, Perl, LAMP, Web. Linux / Windows / OSX. Working for a pretty epic company.
Updated on July 17, 2022Comments
-
Ben almost 2 years
Python (2.6) seems to be derping for no reason, can anyone see a problem with this code?
class DB (): def doSomething (self, str): print str class A (): __db = DB() @staticmethod def getDB (): return A.__db db = property(getDB) A.db.doSomething("blah")
Fails with the exception:
AttributeError: 'property' object has no attribute 'doSomething'
It was my understanding that a property would automatically run its getter when accessed, so why is it complaining about a property object, and why isn't it finding my clearly available method?