[Tutor] Dumb Subclassing question

Chad Crabtree flaxeater at yahoo.com
Thu Aug 5 22:21:58 CEST 2004


I must say this is one of the most neeto hacks I've seen in a while. 

And it's very understandable with your comments.  *applaud*

> from path import path
>
> # Here is the new subclass
> class MyPath(path):
>     def coolStuff(self):
>         print 'It works!'
>
> # Remember the old __new__ so we can delegate to it
> originalNew = path.__new__
>
> # This is going to be the new path.__new__. It will return an
instance 
> of MyPath
> def newNew(cls, *args, **kwds):
>     return originalNew(MyPath, *args, **kwds)
>
> # Replace path.__new__ with our new version
> path.__new__ = staticmethod(newNew)
>
> # Try it out
> p = path("C:")
> print type(p)
> p.coolStuff()
>
> f = p.files()
> print f[0]
> print type(f[0])
>
> Running this program prints
> <class '__main__.MyPath'>
> It works!
> C:.DS_Store
> <class '__main__.MyPath'>
>


		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail


More information about the Tutor mailing list