getting attributes and methods of class without creating object

Terry Reedy tjreedy at udel.edu
Tue May 18 12:03:55 EDT 2010


On 5/18/2010 2:11 AM, shuvro wrote:
>> to know the attributes of an instance of a class without
>> actually creating that instance is very difficult.

> Is it not possible with the existing python facilities (without adding
> external libraries) ?

To know what a function would do if you were to call it, without 
actually calling it, look at the function code with the dis (assembler) 
module.

#3.1
 >>> import dis
 >>> class C:
	def __init__(self, a): self.a = a
	
 >>> dis.dis(C.__init__)
   2           0 LOAD_FAST                1 (a)
               3 LOAD_FAST                0 (self)
               6 STORE_ATTR               0 (a)
               9 LOAD_CONST               0 (None)
              12 RETURN_VALUE

If you read the doc, or know assembler, you would see that the instance 
gets attribute a. Use dir(C) to find all the methods to disassemble.

Terry Jan Reedy




More information about the Python-list mailing list