Ruby Impressions

Neil Schemenauer nas at python.ca
Sat Jan 12 14:14:17 EST 2002


Assuming I understand the Ruby code, this is a reasonable approximation
to it:

    class Base:
        def __init__(self, *args):
            varnames = self.init.func_code.co_varnames
            for name, value in zip(varnames[1:], args):
                setattr(self, name, value)
            self.init(*args)

    class Person(Base):
        def init(self, name, age, gender):
            print "running init"

    p = Person('John', 32, 'M')
    print "name", p.name
    print "age", p.age
    print "gender", p.gender

It could be done a little more efficiently with a Metaclass.

  Neil




More information about the Python-list mailing list