1

I wanted to know whether an object has a copy of all its class methods. So I created a class person, defined a method getName and created an object Harry. I used dir function to check whether both the class and the object have getName method stored inside them and both of them returned true. I then printed out the getName method of both the class and the object and they returned different addresses, but I noticed that harry.getName method is bounded to person.getName. So does that mean that harry.getName is not a copy of its class method rather just a reference to its class method. To check this used delattr method to delete the getName attribute of the person class and again used dir to check it. And as expected it deleted it from both the class person and object Harry.

So please review my code and tell whether my thoughts are correct.

Note:

The dir() function returns all properties and methods of the specified object, without the values.

The delattr() method is used to delete the named attribute from the object, with the prior permission of the object.

Python Code

Karthik
  • 175
  • 1
  • 1
  • 11

2 Answers2

2

Without going into too much detail, what you are observing is the on-the-fly creation of a function that acts like person.getName but the first argument (self) is already bound to an instance.

So no, objects do not have a copy of the methods in their class. Every time you access Harry.getName a new callable object (the "method") is built from scratch. It behaves like person.getName with self=Harry.

On the other hand, no new callable is constructed when you access getName on the class level.

timgeb
  • 76,762
  • 20
  • 123
  • 145
0

What I conclude is this:

When we instantiate a class using variableName = className(args) it creates an object in memory, initializes all its data attributes using init method (constructor) and returns the memory address of the object data to the object variable name and we call it as an instance.

The object data contains two things

  1. Data attributes and its values
  2. Memory references of its class methods.

Whenever we call any method on the object by instanceName.method(), it refers to the class method and passes the object data itself as specified by the self argument and execute it to access or modify the object data.

Thus, the object in the memory only contains data attribute values and not a copy of its class methods, but only their memory references.

Karthik
  • 175
  • 1
  • 1
  • 11
  • 1
    You will get pretty far with that mental model. There are some details to add. For example, when you do `instanceName.method()`, `self=instanceName` is not passed to the function `method` in the classes' `__dict__` directly. A brand new callable is built from scratch on the basis of `className.method` each time you access `instanceName.method`. That brand new callable behaves as if `self=instanceName`. – timgeb May 31 '20 at 07:59
  • 1
    Also, there are no "memory attributes". When Python can't find an attribute in the `__dict__` of an instance, it will go search for it in other places, starting at the class of the instance. – timgeb May 31 '20 at 08:00
  • @timgeb Oh! So the object doesn't have memory attributes. So, it just searches for the called method in the class of the instance and if not found, it searches for it in its parent classes. Is that how inheritance works? – Karthik May 31 '20 at 09:24
  • @timgeb Your answers are very insightful. Thanks . – Karthik May 31 '20 at 09:27
  • 1
    "Is that how inheritance works?" <- [Pretty much](https://stackoverflow.com/questions/2010692/what-does-mro-do) – timgeb May 31 '20 at 09:42