Arguments and attributes with the same name

Ary Borenszweig ary at esperanto.org.ar
Thu Mar 4 07:09:36 PST 2010


bearophile wrote:
> Ary Borenszweig:
> 
>> This can be solved if the compiler warns when an assignment is from a 
>> variable to the same one.
>>
>> x = x; // error: assignment has no effect
> 
> That's nice in general to have, but I think it's not enough, because if I want to assign to an attribute an incremented value:
> x = x+1;
> What is that x? The attribute or the argument?
> 
> Bye,
> bearophile

class Foo:
   def __init__(self, x):
     x = x + 1

This compiles just fine in pyhtoh.

What is x here? The argument, of course, because that's how it works in 
python. You forgot to put self before x.

class Foo {
	int x;
	this(int x) {
		x = x + 1;
	}
}
What is x here? The argument, of course, because that's how it works in 
python. You forgot to put this before x.

How is python safer than D in that sense?



More information about the Digitalmars-d mailing list