2

Python's class instance objects have attributes.Those attributes can be data attributes or methods.

Lets take for example this class

class Foo:

    def bar(self):
        print("FooBar")

And lets create a class instance

fooInstance = Foo()

As i have read from docs and tested by myself i realized that each time i reference in code or each time i call bar from an instance

fooInstance.bar

An method object is created internally every single time.

My question is why doesn’t it being cached and reused

One assumption of mine is because the callable object of the method object could somehow change address internally (i.e. due to reallocations?) and the pointer to it can be invalidated.

Sorry if this question is "silly", i am completely new to Python so my knowledge on it is little, even less on its internals.

0

2 Answers 2

2

Python functions are descriptors. You can check this by simply looking at it's dir:

>>> def fn(self):
...     pass
... 
>>> dir(fn)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
>>> fn.__get__
<method-wrapper '__get__' of function object at 0x7fa5585550c8>

Notice the __get__ method. We can use this to produce bound methods:

>>> class Bar(object): pass
... 
>>> b = Bar()
>>> fn.__get__(b, Bar)
<bound method Bar.fn of <__main__.Bar object at 0x7fa5585cdcd0>>

In fact, this is what python does for you automatically when you access an attribute on a class which is a function.

Now, on to the problem with caching -- functions can be put onto classes from anywhere. I can add a bound method to any class just as I did above -- simple assign b.fn = fn.__get__(b, Bar) and now the instance b has a bound method fn even though none of the other instances of Bar have that method. And I can do this for multiple classes. If the descriptor was to cache the values, it would need to keep a lookup-table that looked up instances and classes to see if it had already created a bound method for that instance and class. Here's a simpler example:

def fn(self):
    return self

class A(object):
    fn = fn

class B(object):
    fn = fn

Notice that A and B hold a reference to the same function -- and they'll both produce bound methods as necessary when that attribute is accessed on an instance.

The lookup table is problematic to construct in the first place as self may not be hashible. I think this is probably the primary reason for creating new instances every time. Even if that wasn't the case, the lookup table would need to store weakrefs -- not actual references -- so that the whole thing could be garbage collected if necessary. I'm guessing that creating a new bound method is probably nearly as quick as doing the lookup and resolution when working with the weakrefs.

0

All you have there is a method with no parameters every time you call a method it will execute. A method variable would be assigning that method into something else to call it later.

If you had a variable with a counter and you added one more to that method every time you can print out and see how it gets modified.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.