Re: python newbie



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

----------------------------------

import callable
callable('test')
Called with ('test',) args, {} kw
callable
<module 'callable' from 'callable.py'>
.



Relevant Pages

  • Re: Halo characte in DOA4?
    ... you've never seen Gordon Freeman in any other sort of game... ... >> (Oops. ...
    (alt.games.video.xbox)
  • Re: Need Help deleting record from text file
    ... oops... ... copy and paste error: ... To sort on file names.... ... I am now using arrays which is a little faster. ...
    (microsoft.public.vb.general.discussion)
  • Re: Just a tease, my friends ...
    ... Must be some sort of Bally machine... ... not too savy on my williams/bally game identification. ...
    (rec.games.pinball)
  • Re: whats the best way to do this?
    ... oops. ... work and the second one has some sort of syntax error in it. ... that's not the same as working pairwise. ...
    (comp.lang.lisp)
  • Re: State machine representation
    ... In article, Dave Roberts ... >> Oops. ... > This seems like one of the areas where CL sort of botched this up. ... There are good reasons why CL does it the way it does, ...
    (comp.lang.lisp)