Favorite non-python language trick?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Jun 24 10:02:53 EDT 2005


On Fri, 24 Jun 2005 06:13:00 -0700, utabintarbo wrote:

>>with colour do begin
>>red := 0; blue := 255; green := 0;
>>end;
>>
>>instead of:
>>
>>colour.red := 0; colour.blue := 255; colour.green := 0;
> 
> Why not:
> 
> from color import *
> 
> red := 0
> blue := 255
> green := 0

Because colour is not a module, it is a record. Sorry, I assumed that
people would be familiar with Pascal records (similar to C structs) and
it wouldn't need explanation. My bad :-(

The closest example in Python would be:

def class Colour:
    def __init__(self, blue=0, green=0, red=0):
        self.blue = blue
        self.green = green
        self.red = red

which would become something like:

def class Colour:
    def __init__(self, blue=0, green=0, red=0):
        # pseudo-Python code borrowing concept "with" from Pascal
        with self:
            blue = blue
            green = green
            red = red

And now you can see why Python doesn't support this idiom.



-- 
Steven.




More information about the Python-list mailing list