More random python observations from a perl programmer
Robb Shecter
shecter at darmstadt.gmd.de
Thu Aug 19 13:32:24 EDT 1999
Jeremy Hylton wrote:
>
> You can do almost exactly this in Python using the update method on
> dictionaries.
>
> x = {1:2, 3:4}
> y = {10:20, 30:40}
> z = {}
> z.update(x)
> z.update(y)
> print z
> {30: 40, 3: 4, 10: 20, 1: 2}
>
> As you noted with the [].sort operation, you need to explicitly create
> the new object that call some methods on it. In my book it is at
> worst a minor annoyance.
In my book it's the right way to do things. My book, by the way, is
"Object Oriented Software Construction", by Bertrand Meyer. He makes
the great point for developing classes that work in a consistent
style:
1) You instantiate an object
2) You set some options
3) You tell it to do its thing
This pays off big in clarity of code. Related to this are his other
commandments, such as banning functions with side effects.
Perl's version:
%z = (%x, %y);
is ambiguous. It looks more like it creates a nested structure than a
flat one.
- Robb
More information about the Python-list
mailing list