Arguments and attributes with the same name

bearophile bearophileHUGS at lycos.com
Thu Mar 4 07:24:27 PST 2010


Ary Borenszweig:
> How is python safer than D in that sense?

This is correct D code:

class Foo {
    int x;
    this() { x = 5; }
}
void main() {}

In Python "self." is always needed to refer to the attribute, and "x" refers to the argument (or a global name, but then you need a global statement). So the eyes of Python programmers are trained to remember such difference.

In D the "this." is optional, even in the constructor. So it's more ambiguous, you have to look at the signature of the method you are inside (or the IDE has to color your names differently).

Bye,
bearophile



More information about the Digitalmars-d mailing list