Other notes

Steven Bethard steven.bethard at gmail.com
Tue Dec 28 23:37:15 EST 2004

bearophileHUGS at lycos.com wrote:
> 4) The printf-style formatting is powerful, but I still think it's
> quite complex for usual purposes, and I usually have to look its syntax
> in the docs. I think the Pascal syntax is nice and simpler to remember
> (especially for someone with a little Pascal/Delphi experience ^_^), it
> uses two ":" to format the floating point numbers (the second :number
> is optional). For example this Delphi program:
> const a = -12345.67890;
> begin
> writeln(a);
> writeln(a:2:0);
> writeln(a:4:2);
> writeln(a:4:20);
> writeln(a:12:2);
> end.
> Gives:
> -1.23456789000000E+0004
> -12346
> -12345.68
> -12345.67890000000000000000
> -12345.68
> (The last line starts with 3 spaces)

Even after looking at your example, I have no idea what the two numbers 
on each side of the :'s do.  The last number appears to be the number of 
decimal places to round to, but I don't know what the first number does.

Since I can't figure it out intuitively (even with examples), I don't 
think this syntax is any less inscrutable than '%<width>.<decimals>f'. 
My suspicion is that you're just biased by your previous use of Pascal. 
  (Note that I never used Pascal or enough C to use string formatting 
before I used Python, so I'm less biased than others may be in this 

> 6) map( function, list, ...) applies function to every item of list and
> return a list of the results. If list is a nested data structure, map
> applies function to just the upper level objects.
> In Mathematica there is another parameter to specify the "level" of the
> apply.
> So:
> map(str, [[1,[2]], 3])
> ==>
> ['[1, [2]]', '3']
> With a hypothetical level (default level = 1, it gives the normal
> Python map):
> map(str, [[1,[2]], 3], level=1)
> ==>
> ['[1, [2]]', '3']
> map(str, [[1,[2]], 3], level=2)
> ==>
> ['1', '[2]', '3']
> I think this semantic can be extended:
> level=0 means that the map is performed up to the leaves (0 means
> infinitum, this isn't nice, but it can be useful because I think Python
> doesn't contain a built-in Infinite).
> level=-1 means that the map is performed to the level just before the
> leaves.
> Level=-n means that the map is performed n levels before the leaves.

This packs two things into map -- the true mapping behavior (applying a 
function to a list) and the flattening of a list.  Why don't you lobby 
for a builtin flatten instead?  (Also, Google for flatten in the 
python-list -- you should find a recent thread about it.)

> 10) There can be something in the middle between the def statement and
> the lambda. For example it can be called "fun" (or it can be called
> "def" still). With it maybe both def and lambdas aren't necessary
> anymore. Examples:
> cube = fun x:
> return x**3
> (the last line is indented)
> sorted(data, fun x,y: return x-y)
> (Probably now it's almost impossible to modify this in the language.)

Google the python-list for 'anonymous function' or 'anyonymous def' and 
you'll find a ton of discussion about this kind of thing.  I'll note 
only that your first example gains nothing over

     def cube(x):
         return x**3

and that your second example gains nothing over

     sorted(data, lambda x, y: return x-y)


     sorted(data, operator.sub)

> 11) This is just a wild idea for an alternative syntax to specify a
> global variable inside a function. From:
> def foo(x):
> global y
> y = y + 2
> (the last two lines are indented)
> To:
> def foo(x): global.y = global.y + 2

Well, you can do:

def foo(x):
     globals()['y'] = globals()['y'] + 2

Not exactly the same syntax, but pretty close.

> 13) In Mathematica language the = has the same meaning of Python, but
> := is different:
> lhs := rhs assigns rhs to be the delayed value of lhs. rhs is
> maintained in an unevaluated form. When lhs appears, it is replaced by
> rhs, evaluated afresh each time.
> I don't know if this can be useful...

You can almost get this behavior with lambdas, e.g.:

    x = lambda: delayed_expression()

then you can get a new instance of the expression by simply doing:

    new_instance = x()

I know this isn't exactly what you're asking for, but this is one 
current possibility that does something similar.  You might also look at:


which suggests a simpler syntax for this kind of usage.


More information about the Python-list mailing list