Type unification and modules

Pekka Pessi Pekka.Pessi at nokia.com
Sun Sep 9 14:49:58 EDT 2001


In message <cplmjpnefz.fsf at cj20424-a.reston1.va.home.com> Guido van Rossum <guido at python.org> writes:
>> 	Now that Python types and classes are being unified, should the
>> 	exceptions about modules be removed, too?  I mean things like
>> 	__call__, __getattr__, or __setattr__ not working.

>I'm not sure what you mean; can you elaborate with an example?  

	Sorry, my mistake.  __setattr__ and __getattr__ seem to be
	working as expected.

	However, I the __call__ attribute does not work (or then I have
	an old alpha):

Python 2.2a2 (#1, Sep  3 2001, 23:06:29) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-85)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import types
>>> class Module(types.ModuleType):
...  pass
... 
>>> m = Module()
>>> def modulecall():
...  print "called a module"
... 
>>> m.__call__ = modulecall
>>> m()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object is not callable: <module '?' (built-in)>
>>> class Class:
...   pass
... 
>>> c = Class()
>>> def instancecall():print "called an instance"
... 
>>> c.__call__ = instancecall
>>> c()
called an instance
>>> 

	I thought that modules were exception to the rule, but only pure
	instances seem to support __call__ now.  There seems to be an
	IsInstance() test somewhere preventing calling other objects.

>Do you want to be able to call a module? Why?

	Actually, I want to call *any* object with a __call__ attribute.

	Modules are only way to access other languages; if I have a
	module which just provides access to an object implemented in C,
	I'd like to create that object just by calling the module.

	On the other hand, now that type objects can be are used as
	constructors, I should call the type object instead. Hmmm.

>The __getattr__ and __setattr__ methods work on modules, but I'm not
>sure if that's what you are thinking about:

>    >>> import sys
>    >>> sys.__getattr__("path")
>    [...something...]

>Do you want to be able to override __getattr__ and __setattr__ in
>modules?  That has nothing to do with the type/class unification; a
>module is still not the same as a class.  But you could write a
>subclass of the module type that supported any special method you
>like.

	Yes, you are right.  I just screwed my test code, I used
	__[sg]etattr__ as instance attributes.

						Pekka



More information about the Python-list mailing list