Favorite non-python language trick?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Jun 26 00:08:12 EDT 2005


On Sat, 25 Jun 2005 18:44:12 -0700, James Stroud wrote:

> On Saturday 25 June 2005 11:08 am, Steven D'Aprano wrote:
>> The problem is, you have made colour (returning to English spelling
>> instead of foreign) into a class. If you need two colour variables, you
>> have to duplicate the code for the class (perhaps only changing the
>> numeric constants. You can't even make instances from the class, because
>> they all share the same RGB values, which is pretty useless if they are
>> meant to represent different colours.
> 
> py> class color:
> ...   pass
> ...
> py> def with(classname, **kwargs):
> ...   classname.__dict__.update(kwargs)
> ...
> py> with(color, red=0,
> ...             blue=255,
> ...             green=255)
> py>
> py> color.blue
> 255
> 
> Is this what you want to do?

No it isn't. That doesn't help at all if you want to do something
other than assigning to instance attributes.

For starters, it doesn't help me do this:

with colour:
    print "Blue =", .blue
    print "Green =", .green
    print "Red =", .red
    
or anything more complex:

with my_directory_object:
    L = .raw_paths
    L.append("~/custom/")
    # move the last directory looked to the front
    # and the current directory to the end
    .normalise(L, first=.last_dir, last=".")
    .paths = L
    try:
        .currentpos += 1
    except AttributeError:
        .currentpos = 0

The point is that a hypothetical "with" block would have to allow
arbitrary access to dotted names: getting, setting, deletion, and method
calling, not just one very limited form of keyword assignment.



>> > Also avoids those stupid little colons.
>>
>> Using := and = for assignment and equality is precisely as stupid as using
>> = and == for assignment and equality. Perhaps less stupid: why do we use
>> == for equals, but not ++ for plus and -- for minus?
> 
> The colons are to make it look more official or something. 

That is the most ridiculous thing I have ever heard about a programming
language.

Has it escaped your notice that every language has to distinguish in
some way between "x equals 1" in the sense of assignment and "x equals 1"
in the sense of comparisons? "x = 1" is ambiguous.

A language might use constructs like:

"SET x = 1" and "x = 1" 

for assignment and comparison, or do what Python does:

"x = 1" and "x == 1"

or do what Pascal does:

"x := 1" and "x = 1"

Even if the compiler can always unfailing tell them apart from context,
for the sake of the human programmers who have to read and write the code,
it is important to have two different operators.


> They are useless and hence stupid. 

Useless huh? Just try leaving them out and see how useless they are.

> The two equals signs for comparison could be argued to be 
> redundant, 

No they aren't redundant, because assignment and equality testing are very
different. And in Python, you can even use them in the same place, so you
can't expect the compiler to tell them apart from context.

py> x = 2  # did I mean assignment or comparison?

> but the colons are absolutely unecessary. I thought they were 
> pointless 18 years ago when I learned pascal in highschool and after 20 
> years, I still think they are still pointless.

You didn't learn Pascal very well then if you can't see the difference
between assignment and equality testing.


-- 
Steven.




More information about the Python-list mailing list