another syntax we don't need (initializers)

Jeff Epler jepler at unpythonic.net
Mon Dec 9 14:38:13 EST 2002


Some people might miss C++'s initializers, which are written like
    class Foo {
     public:
        double x;
        Foo(_x) : x(_x) {}
    }

We could add syntax to Python to support something like this:
    class Foo:
        def __init__(self, self.x): pass

when an item in a parameter list is a dotted name, the corresponding
argument is assigned to the dotted name and does not appear in locals.

Keyword and default arguments would work in the natural way:
    class Bar:
        def __init__(self, self.x=3, self.y=4): pass

    # equivalent to b = Bar(0, 1)
    b = Bar(y=1, x=0)

    # equivalent to c = Bar(0, 4):
    c = Bar(0)

The syntax could also be used in a setter function:
    class Baz:
        def set_z(self, self.z): pass

    d = Baz()
    d.set_z(3) # equivalent to 'd.z = 3'

Name mangling takes place in the standard way, so that
    def set_z(self, self.__z): pass
will set the same attribute as
    def set_z(self, z): self.__z = z

Jeff




More information about the Python-list mailing list