Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a property method callable

I have a class with a property method:

class SomeClass(object):

    @property
    def some_method(self):

        return True

I want to make a call to some_method and store it in as an expression.

For example:

some_expr = SomeClass().some_method

Should not store the value True in some_expr; rather should store the expression SomeClass().some_method so that it is callable.

How can I implement this?

like image 386
Vishal K Nair Avatar asked Nov 24 '25 18:11

Vishal K Nair


1 Answers

Note, you essentially want to use the .__get__ callable on the property instance. However, this will require an instance as the first argument, so you could just partially apply some instance:

In [6]: class SomeClass(object):
   ...:
   ...:     @property
   ...:     def some_method(self):
   ...:
   ...:         return True
   ...:

In [7]: from functools import partial

Then something like:

In [9]: some_exp = partial(SomeClass.some_method.__get__, SomeClass())

In [10]: some_exp()
Out[10]: True
like image 87
juanpa.arrivillaga Avatar answered Nov 26 '25 12:11

juanpa.arrivillaga