class declaration shortcut

Steven Bethard steven.bethard at gmail.com
Wed Feb 28 16:21:05 EST 2007


Luis M. González wrote:
> I've come across a code snippet in www.rubyclr.com where they show how
> easy it is to declare a class compared to equivalent code in c#.
> I wonder if there is any way to emulate this in Python.
> 
> The code is as follows:
> 
> Person = struct.new( :name, :birthday, :children)

How about something like::

     class Person(Record):
         __slots__ = 'name', 'birthday', 'children'

You can then use the class like::

     person = Person('Steve', 'April 25', [])
     assert person.name == 'Steve'
     assert person.birthday == 'April 25'
     assert not person.children

Is that what you were looking for?  If so, the recipe for the Record 
class is here:

     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502237

STeVe



More information about the Python-list mailing list