tags:

views:

53

answers:

1

Having a class like this:

class Spam(object):
   def __init__(self, name=''):
      self.name = name

eggs = Spam('systempuntoout')

using dis, is it possible to see how an instance of a class and the respective hex Identity are created?

+1  A: 

Yes, but it isn't obvious from the output, which is at the level of Python bytecode, e.g.:

>>> class Foo(object):
...   def f(x): return x * x
... 
>>> dis.dis(Foo)
Disassembly of f:
  2           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                0 (x)
              6 BINARY_MULTIPLY     
              7 RETURN_VALUE        

It doesn't take much to figure out what Foo.f is doing from the above dump, but it quickly becomes unreadable to most people as the size of the code grows.

Marcelo Cantos
Sorry Marcelo but i don't understand your answer, does it anwser my question in any way :)?
systempuntoout
Perhaps I misunderstood your question. The `dis` module shows you the bytecode of compiled functions and methods. If you call `dis.dis(Spam)` with your class, you will see the bytecode implementation of your init method. It this what you want, or are you looking for something else?
Marcelo Cantos
Yes, i was talking about dis.dis(Spam) thanks.Is it possible to see when object ID is created?
systempuntoout
If I recall correctly, the object's id is simply its memory address cast as an integer.
Marcelo Cantos