Hack request: rational numbers

Hans Nowak ivnowa at hvision.nl
Fri Jan 26 08:37:02 EST 2001


Alex Martelli wrote:

> r3/4 would work if your module could have a __getattr__ (and if
> you could reserve in your modules all variables of the form 'r\d+'),
> as would 3/r4.  I suspect you stand a slightly better chance to
> get some form of __getattr__ (and __setattr__) into modules, which
> would be good for this & other uses, than specialized syntax for
> rational numbers -- Moshe Zadka had urged me to write a PEP about
> it (though I demurred when I found out that sys.modules CAN at
> present house class-instances).

Cool, I didn't know that! You can actually do things like:

>>> class FakeModule:
	def __getattr__(self, name):
		print name, "yourself, you insolent whelp!"
	def __repr__(self):
		return "<module fake at %s>" % hex(id(self))

	
>>> sys.modules['fake'] = FakeModule()
>>> import fake
>>> fake
<module fake at 0xd7c7cc>
>>> fake.foo
foo yourself, you insolent whelp!

So you can actually import something that is not a module. Now how can I
abuse this... hmm... :)

(*ponders*) Maybe we will get to the point that everything will be a
class with a full set of user-definable and magic methods... the
function attributes of 2.1 seem a step in that direction, at least.
Adding attributes to everything else is not so far away; neither is
adding user-definable and magic methods to everything. (Whether it will
really be implemented is a different can of spam, of course.)



More information about the Python-list mailing list