开发者

How to Disassemble an object creation in Python?

开发者 https://www.devze.com 2022-12-28 02:30 出处:网络
Having a class like this: class Spam(object): def __init__(self, name=\'\'): self.开发者_StackOverflow中文版name = name

Having a class like this:

class Spam(object):
   def __init__(self, name=''):
      self.开发者_StackOverflow中文版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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消