problems with the types module

Michele Simionato mis6 at pitt.edu
Wed Dec 11 14:23:06 EST 2002


Consider the following script:

------ begin types1.py -----

from types import *

class C(object):
      def m(self,x): return x*x
      assert type(m) is FunctionType
      print 'type of m inside its class:',type(m)

print 'type of C.m:', type(C.m)
assert type(C.m) is MethodType

------ end types1.py -----

The output is 

type of m inside its class: <type 'function'>
type of C.m: <type 'instance method'>

i.e. the same object is seen as a function inside the class scope and as an
instance method outside the class scope. To me, this fact was quite surprising,
I don't like to have a changing type depending on the context. Maybe a unique 
'function' type for both functions and methods would have been a simpler
solution.

In any case, this not such a big problem. However, consider the following
script, where static methods appear:

------ begin types2.py -----

from types import *

class C(object):
      def m(self,x): return x*x
      m=staticmethod(m)
      #assert type(m) is ?????
      print 'type of m inside its class:',type(m)

print 'type of C.m:', type(C.m)
assert type(C.m) is MethodType

------ end types2.py -----

The output of this script is  

type of m inside its class: <type 'staticmethod'>
type of C.m: <type 'function'>

Now, C.m is no more an instance method, but it is a function (I understand 
the logic in that), whereas the type of m is ?????  
By ????? I mean that in the types modules there is no StaticMethodType,
I couldn't find any correct assertion for the type of m !
According to the documentation in types the following names are defined: 

NoneType 
TypeType 
IntType 
LongType
FloatType 
ComplexType 
StringType 
UnicodeType 
TupleType 
ListType 
DictType 
DictionaryType 
FunctionType 
LambdaType 
GeneratorType 
CodeType 
ClassType 
InstanceType
MethodType 
UnboundMethodType
BuiltinFunctionType
BuiltinMethodType
ModuleType
FileType 
XRangeType
SliceType 
EllipsisType 
TracebackType
FrameType 
BufferType 
StringTypes 

There is no StaticMethodType. At the best, this in inconsistent with the
output of type(m) when invoked in its class. I am looking forward for
reactions and comments, yours

--
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/



More information about the Python-list mailing list