PEP for an unrestricted __init__ ?!
Jeff Shannon
jeff at ccvcorp.com
Fri Apr 19 13:57:19 EDT 2002
In article <mailman.1019182657.12773.python-list at python.org>,
pyth at devel.trillke.net says...
> But __new__ or __init__ still restrict you to return instances
> of their respective class? This does sound rather sane,
> but i imagined some other uses for an unrestricted __init__ (or __new__)
> such as making functions-objects a special case of classes:
>
> def func(args):
> "compute some stuff
> return result
> and
> class func:
> def __init__(self, args):
> "compute some stuff"
> return result
Well, you can already accomplish something like what you are
asking for, by defining a __call__() method for the class. That
lets class instances be used as callable objects -- using
function syntax on them.
>>> class foo:
... def __init__(self, food):
... self.food = food
... def __call__(self):
... print "Spam and %s!" % self.food
...
>>> eggs = foo('eggs')
>>> beans = foo('baked beans')
>>> eggs()
Spam and eggs!
>>> beans()
Spam and baked beans!
>>> beans.food = 'bacon'
>>> beans()
Spam and bacon!
>>>
Obviously, you can do any amount of processing and checking that
you want in your __call__() method.
So most of what you want to do, already exists. :) (Guido's
time machine strikes again!)
--
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list