Python Forum
Do objects get their own copy of the class methods?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do objects get their own copy of the class methods?

Platform: Windows 10
Python version: 3.7

I came across a video on YouTube which suggested that objects

in Python get their own copies of the class methods.

If this is true then programs in Python must use a lot of memory.

The code below was an attempt to print the ids of the two methods.

The result was that both methods had the same id.

Would appreciate some clarification on this issue.

Thanks in advance for any help.

class Testing:
    def print_me(self):        
    def print_me2(self):        
myTest = Testing()
Your code doesn't test what you are asking about. You are asking about the same method of two different instances. You are testing two different methods of the same instance.

That said, I'm not sure what is going on with your results. The id of an object should be unique to that object and constant over it's lifespan. That two methods of the same instance have the same id means either they're the same object or their lifespans do not overlap.

I assigned the methods to other variables, and got different id's. That seems to imply that bound methods are created as needed. If you create one just for an id check, that id is freed up after the id check because the as-needed bound method goes away. That id is then reused for the second id check. However if you save the as-need bound method with a variable, a new id has to be picked for it.

I don't know for sure, that's just guessing based on the observed behavior.

Note that if you separately store the same method of the same instance in two different variables, they have different ids.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Possibly Related Threads…
Thread Author Replies Views Last Post
  access share attributed among several class methods drSlump 0 245 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 698 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 1,043 May-10-2021, 01:46 AM
Last Post: deanhystad
  too many methods in class - redesign idea? Phaze90 3 1,186 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 1,047 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  cant able to make methods interact with each other in the class jagasrik 2 793 Sep-16-2020, 06:52 PM
Last Post: deanhystad
  Class objects Python_User 12 2,075 Aug-27-2020, 08:02 PM
Last Post: Python_User
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 868 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  printing class properties from numpy.ndarrays of objects pjfarley3 2 991 Jun-08-2020, 05:30 AM
Last Post: pjfarley3
  Question about naming variables in class methods sShadowSerpent 1 956 Mar-25-2020, 04:51 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020