Honey Web

about all things sweet and web-ish

More objects.

leave a comment »

Puffin staring

Puffin staring

Isn’t the puffin just great? Hopefully he’ll be enough to distract you from the rest of the post.

 

I’ve done some more work on the object system. It now has amazing features, such as attribute retrieval and descriptors. I’ve refactored much of it, and of course I broke many tests. With NotFound and whiteknight’s help I added support for better syntax for attribute retrieval (obj.attr instead of obj.get(‘attr’)) and fixed some long-standing issues. My thanks to everyone else that gave me a hand while stumbling about, debugging.

 

Attribute retrieval in Python isn’t as simple as you might think. When retrieving an attribute on an object, first the object’s __dict__ is searched. If the attribute isn’t there, the object’s __class__ is checked for the attribute (which in turn will check __class__.__dict__). If that fails, the object’s class’ bases (parents) are checked.

That isn’t the end of the story, though. This logic is implemented in the __getattribute__ special method. ‘object’, the parent of all objects, implements __getattribute__. Hence, it can be overridden in your own objects (usually a bad idea, though). But this isn’t the end of the story either.

In case you don’t know, everything after the dot in Python is an attribute  (obj.attr_name), including callables. To support something that looks like methods, Python uses descriptors to create bound methods, which are essentially closures over ‘self’. Descriptors are a general concept however, and they’re also used to implement properties. They are implemented in object.__getattribute__.

So if you went through the trouble of reading those links to that awesome book, you are likely to share my opinion: that for its features, Python’s object system is simple, straightforward and with much of its implementation exposed to applications.

 

I have implemented a proto-instance, ‘object’, ‘type’, ‘function’, and partially implemented ‘tuple’ and ‘int’. All of these have tests that largely pass, my biggest culprit is __getattribute__. Also, my function takes in a parrot Sub as its second parameter, so it should be easy to access all of parrot’s features directly from python later on.

I believe I’ve implemented enough of the object system for it to be a useful target to the compiler, so I’ll start retargeting it from PIR types to this object system. While the object system isn’t entirely correct yet and still very incomplete, I believe both these issues can be fixed later, during implementation.

About these ads

Written by Lucian

June 10, 2011 at 2:15 pm

Posted in parrot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: