Hacker News new | past | comments | ask | show | jobs | submit login
Python traits: improving composability of object oriented programming (enthought.com)
2 points by yummyfajitas on March 15, 2008 | hide | past | favorite | 1 comment



It's hard to summarize traits in the headline.

Here is an academic paper describing them:

http://www.iam.unibe.ch/~scg/Archive/Papers/Scha03aTraits.pd...

They were originally built for smalltalk, but apparently there is now a python library. I gave it a try, it's actually pretty nice.

Basically, traits provide reusable components of behavior that are not tied to classes. It's pretty sweet. It lets you do things like delegate data members/methods to other objects.

For instance:

    class employee(HasTraits):
        employer=Trait(company)

    class manager(employee):
        hire=Delegate('employer','hire')
This code causes an employee to have an employer data member, which MUST be a company object. So far, just static typing (checked at runtime).

The manager subclass of an employee has a "hire" method. But a manager doesn't hire employees himself; instead, he hires employees on behalf of his employer. So boss.hire(emp) is delegated to the boss's employer, and is equivalent to boss.employer.hire(emp).

It has lots of other cute features like this as well. Take a look.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: