Re: python newbie
- From: Duncan Booth <duncan.booth@xxxxxxxxxxxxxxx>
- Date: 3 Nov 2007 15:58:49 GMT
Paul Rubin <http://phr.cx@xxxxxxxxxxxxxx> wrote:
Duncan Booth <duncan.booth@xxxxxxxxxxxxxxx> writes:
modules are not special in any way, except that you cannot subclass
them. Oops, sorry I got that wrong. Modules are not special in any
way, they can have methods as well as functions:
I've felt for a long time that you should be able to define __call__
on a module, but that doesn't seem to be allowed, at least if you
try it in the obvious way.
This isn't perfect (global variables have to be set before hooking the
module) but it sort of works:
--------- callable.py ---------------
"""How to define a callable module"""
import sys, new
class CallableModule(new.module):
def __call__(self, *args, **kw):
self._call_(*args, **kw)
def _call_(*args, **kw):
"""A call method"""
print "Called with %r args, %r kw" % (args, kw)
self = CallableModule(__name__, __doc__)
self.__dict__.update(sys.modules[__name__].__dict__)
sys.modules[__name__] = self
----------------------------------
Called with ('test',) args, {} kwimport callable
callable('test')
<module 'callable' from 'callable.py'>callable
.
- Follow-Ups:
- Re: python newbie
- From: Paul Rubin
- Re: python newbie
- References:
- python newbie
- From: Jim Hendricks
- Re: python newbie
- From: Bjoern Schliessmann
- Re: python newbie
- From: Bruno Desthuilliers
- Re: python newbie
- From: Hendrik van Rooyen
- Re: python newbie
- From: Steven D'Aprano
- Re: python newbie
- From: Duncan Booth
- Re: python newbie
- From: Paul Rubin
- python newbie
- Prev by Date: Re: Is pyparsing really a recursive descent parser?
- Next by Date: Re: MP3 and ID3 library/module recommendations
- Previous by thread: Re: python newbie
- Next by thread: Re: python newbie
- Index(es):
Relevant Pages
|
|