38966

Why the id of an object would change depending on the line in the python shell

This questions is just out of curiosity.

While I was reading the python's object model documentation, I decided to experiment a little with the id of a class method and found this behavior:

Python 3.2.2 (default, Sep 4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> class A(): def a(self): pass >>> id(A().a) 54107080 >>> id(A().a) 54108104 >>> id(A().a) 54107080 >>> id(A().a) 54108104 >>> >>> id(A().a) 54108104 >>> >>> id(A().a) 54108104 >>> id(A().a) 54107080 >>>

The id of the method changes with the parity of the line!

I actually wanted to create a couple of instances of the same class and see if they had the same method object, and I expected that they would be the exact same one, or change every time, what I did not expect was that the method id would be related with the interpreter line being even or not! Any ideas?

Note: I know that there is a mismatch of version from the docs and the interpreter, it just happens that I'm on windows and I have only 3.2 installed

Answer1:

I will explain wat the line id(A().a) does:

A() # creates a new object I call a

Then

A().a # creates a function f bound to a A.a.__get__(A(), A) # same as above >>> A.a.__get__(A(), A) <bound method A.a of <__main__.A object at 0x02D85550>> >>> A().a <bound method A.a of <__main__.A object at 0x02D29410>>

This bound function is always another because it has another object in __self__

>>> a = A() >>> assert a.a.__self__ is a

__self__ will be passed as first argument self to the function A.a

EDIT: This is what it looks like:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> class A: def a(self): pass >>> id(A().a) 43476312 >>> id(A().a) 49018760

Here the id repeats like abab

Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> class A: def a(self): pas s >>> id(A().a) 50195512 >>> id(A().a) 50195832 >>> id(A().a) 50195512 >>> id(A().a) 50195832

EDIT: For linux or what is not my machine and whatsoever I do not know

id(A().a)

will always give the same result except if you store this to a variable. I do not know why but I would think that it is because of performance optimization. For objects on the stack you do not need to allocate new space for an object every time you call a function.

Recommend

  • extrange behavior of serial port using Qt
  • How can I check the parity of a DES key?
  • Serial COM port communications not working
  • Visual Studio 2010 Arduino serial communication
  • How to use Swing Timer ActionListener
  • cd vs !cd vs %cd in IPython
  • Multiprocessing code works upon import, breaks upon being called
  • Adding a field to a structured numpy array (3)
  • Entity framework - Foreign key - data annotation
  • Pass large string into api controller by $http angular service
  • Per Machine App Registration
  • IIS 7.5 404 Error for .PDF files
  • how to load css classes from my own project specfic css in Sitecore's RAD editor?
  • Autohotkey script running program with command line arguments
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Can't locate Module/Build.pm in @INC (@INC contains: /usr/local/lib64/perl5
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Creating a DropDownList
  • Who propagate bugfixes across branches (corporate development)?
  • Grails calculated field in SQL
  • Alternative to overridePendingTransition() - Android
  • Scrapy recursive link crawler
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • How to add date and time under each post in guestbook in google app engine
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • JSON with duplicate key names losing information when parsed
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • Return words with double consecutive letters
  • Comma separated Values
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Revoking OAuth Access Token Results in 404 Not Found
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]