[Tutor] Simple Question On A Method (in subclass)
Steven D'Aprano
steve at pearwood.info
Tue Nov 1 02:33:44 CET 2011
Marc Tompkins wrote:
> It can be a little hard to wrap your head around how Python handles
> variables/objects; in other languages you create a variable and assign a
> value to it, while in Python you create an object and assign a name to it -
> the name can change while the object remains unchanged. Here's a very
> simplified demo of what Dave is talking about:
[...]
> It's extremely logical, but almost entirely backward from the way most other
> languages do things. Possibly it's because Guido is Dutch.
Fortunately, that is untrue.
I'm not sure where the myth that "Python is different from other
languages" comes from. I suppose it is true only so far as *every*
language is different from any other language (otherwise they would be
the same language!). But Python is not so different from other common
languages.
In particular, I don't know of any language where assignment means
aliasing. Take the example Marc gave earlier:
t = 'this'
s = 'that'
group = [t, s]
print group # => "['this', 'that']
s = 'the other'
print group # => "['this', 'that']
I don't know of any language where the second item of group would now
equal 'the other'. Pascal certainly isn't one:
program example (input, output);
var
t,s: String(10);
group: array[1..2] of String(10);
begin
t:='this';
s:='that';
group[1]:=t;
group[2]:=s;
writeln(group[1], ' ', group[2]);
s:='the other';
writeln(group[1], ' ', group[2]);
end.
Running that program gives the equivalent output to Python:
this that
this that
PHP is the same. Using the PHP interactive shell:
php > $t = 'this';
php > $s = 'that';
php > $group[0] = $t;
php > $group[1] = $s;
php > print_r($group);
Array
(
[0] => this
[1] => that
)
php > $t = 'the other';
php > print_r($group);
Array
(
[0] => this
[1] => that
)
This myth of Python being radically different from other languages is
especially mysterious since many of the most popular and common modern
languages use *exactly* the same name binding execution model as Python,
e.g. Java and Ruby. (In the case of Java, that only applies to boxed
objects, and not unboxed low-level ints and similar. If this means
nothing to you, feel fortunate.)
--
Steven
More information about the Tutor
mailing list