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?
#1
Hi,

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):        
        print(id(self.print_me))
        
    def print_me2(self):        
        print(id(self.print_me2))
        
myTest = Testing()
myTest.print_me()
myTest.print_me2() 
Reply
#2
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 - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 401 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  How can I access objects or widgets from one class in another class? Konstantin23 3 929 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Structuring a large class: privite vs public methods 6hearts 3 1,014 May-05-2023, 10:06 AM
Last Post: Gribouillis
  access share attributed among several class methods drSlump 0 1,038 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 2,497 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 2,294 May-10-2021, 01:46 AM
Last Post: deanhystad
  too many methods in class - redesign idea? Phaze90 3 2,450 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 2,220 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  cant able to make methods interact with each other in the class jagasrik 2 1,750 Sep-16-2020, 06:52 PM
Last Post: deanhystad
  Class objects Python_User 12 4,300 Aug-27-2020, 08:02 PM
Last Post: Python_User

Forum Jump:

User Panel Messages

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