What is happening here?
What do the brackets do here test2 = A()?
How can I make object A() callable.
class A(object):
@property
def a(self):
"an important attribute"
return ["a value","dsfsdfsd"]
test1 = A
test2 = A()
print test1().a
print test2().a
(I’ve edited my previous anwser, which was working but as pointed out, not really a good practice)
There is slight confusion between object representation in Python here :
In python you can copy a class, because a class in also an object in Python. You can see as an alias, but it is more useful than that.
Here you have created an A object at a certain adress.
What you are doing is in reality :
A().a. In other word you create a temporary object and then call the a property. It works in your example but it is not more useful than a static method since the object is temporary (usually class methods take are used to view/modify an object’s internal state).print test2().a # == [A()]().aThis does not work because you want to call an object as a method. To do so you have to add a
__call__special method in the class description. If your a property is important, that’s what I would do :