Operator overloading and __getattr__

John Roth newsgroups at jhrothjr.com
Tue Dec 23 17:22:28 CET 2003

"Samuel Kleiner" <sam at h41n2fls31o839.telia.com> wrote in message
news:slrnbue5dp.72h.sam at h41n2fls31o839.telia.com...
> I'm trying to create methods on method access- but __getattr__ fails
> with operator overloading(below) Any suggestions? EG:

I haven't a clue what you're trying to do, but
I believe it's not possible to install "magic methods"
after the class is defined. Nor can you install
them in the instance. They'll look like they've
been installed, but the interpreter will never
execute them because it does not look there
for them. It looks in special places in the C
language structure for the class definition.

John Roth

> class NewM(dict):
>     def __getattr__(self,name):
>         def fi(m):
>             if type(name) in self:
>                 self[type(name)]=self[type(name)].__dict__[name](m)
>             else:
>                 self[type(name)]=m
>         return fi
> p=int(2)
> s=NewM()
> print p.__add__(2)
> print s.__add__(2)
> print p+2
> print s+2
> Causes TypeError on the last line
> -- 
> "I know, there is no Nobel Prize in preternatural biology. That's
>  because Alfred Nobel's wife was seduced by a vampire." --Andrew Plotkin

More information about the Python-list mailing list