Ok, so to round out the educational element…
One would suspect that the OP is in control of whatever is accessing default_image
. It is perfectly reasonable to suspect that whatever that is, it is expecting a method in the class, ie a callable.
class MyClass:
def default_image(self):
return 'the_image.png'
# method that references 'default_image'
def grab_the_image(self):
…
return self.default_image()
So, to access the output of default_image
:
my_img = MyClass().default_image()
Or, we suspect the OP to be using it within a class:
my_img = self.default_image()
Since, in this case, default_image
is returning a string and is not completing any processing/logic in the output of that string, we don’t actually need a method/callable to access that value, we can simply use a variable/literal:
class MyClass:
default_image = 'the_image.png'
Then to access default_image
:
my_img = MyClass().default_image #ie no '()'
Or even, without initialising the class:
my_img = MyClass.default_image #ie no '()' on class either
Or, in the case where some other method in the class wants the default_image
string:
class MyClass:
default_image = 'the_image.png'
…
# method that references 'default_image'
def grab_the_image(self):
…
return self.default_image
The short verion of which is:
If you are accessing a method, then you need to add the ()
to the call, in order to execute that method (otherwise you’ll just get a copy of the method), if you are accessing a literal, then you don’t use the ()
.
If you get that mixed up, then you’ll end up with a lot of errors.
If you are changing the object in the class from a method to a literal, or vice versa, then you will also need to update all of the references.