hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Matz on Python OO #

by why in cult

On his journal, Matz has illustrated some differences between Ruby classes and Python classes, running some examples through the Python interpreter. He largely dwells on the syntax for retrieving a method list for an integer type. I think he’s awfully proud of Ruby’s elegance in this matter, which is cool with me.

In Python:

 >>> (1).__class__.__dict__.keys()
 ['__int__', '__ror__', '__rtruediv__', ... ]

In Ruby:

 >> 1.methods
 => ["%", "between?", "method", "send", ... ]
This just a few days after he mentioned using the encoding pragma from Python’s PEP 263 in a future release of Ruby.
said on 15 Jan 2005 at 13:02

I wonder why he is not just doing dir(1) instead.

I think he’s right in that it’s hard to find an equivalent to 1.class.instance_methods(false), though.

said on 15 Jan 2005 at 19:57

IIRC dir can be tricked in python. But maybe dict can be tricked too. All that stuff like setattr/getattr/getattribute/slots may be related, but I may be wrong

said on 15 Jan 2005 at 20:58

The problem with dir() is that it returns both the attributes AND the methods of the object passed to it. This stems from the fact that in fact methods actually ARE attributes in Python – they just happen to be attributes whose value is a function.

Instead of being called object-oriented, Python should perhaps be called dict-oriented, as it uses dicts and functions to simulate object-orientation. Actually it’s a pretty neat trick – once you grok this a lot of Python’s behaviour makes sense, but I think that Ruby’s real object-orientation is more useful.

Comments are closed for this entry.