473,795 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Other notes

Here are some questions and suggestions of mine that I've collected in
the last weeks on the language (please note that some (most?) of them
are probably wrong/useless/silly, but I've seen that such notes help me
understand a lot of things and to find my weak spots.)

1) I've seen that list pop/append is amortised to its tail, but not for
its head. For this there is deque. But I think dynamical arrays can be
made with some buffer at both head and tail (but you need to keep an
index S to skip the head buffer and you have to use this index every
access to the elements of the list). I think that the most important
design goal in Python built-in data types is flexibility (and safety),
instead of just speed (but dictionaries are speedy ^_^), so why there
are deques instead of lists with both amortised tail&tail operations?
(My hypothesis: to keep list implementation a bit simpler, to avoid
wasting memory for the head buffer, and to keep them a little faster,
avoiding the use of the skip index S).
2) I usually prefer explicit verbose syntax, instead of cryptic symbols
(like decorator syntax), but I like the infix Pascal syntax ".." to
specify a closed interval (a tuple?) of integers or letters (this
syntax doesn't mean to deprecate the range function). It reminds me the
.... syntax sometimes used in mathematics to define a sequence.
Examples:

assert 1..9 == tuple(range(1,1 0))
for i in 1..12: pass
for c in "a".."z": pass
3) I think it can be useful a way to define infix functions, like this
imaginary decorator syntax:

@infix
def interval(x, y): return range(x, y+1) # 2 parameters needed

This may allow:
assert 5 interval 9 == interval(5,9)
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:

{$APPTYPE CONSOLE}
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.2345678900000 0E+0004
-12346
-12345.68
-12345.678900000 00000000000
-12345.68
(The last line starts with 3 spaces)
5) From the docs about round:
Values are rounded to the closest multiple of 10 to the power minus n;
if two multiples are equally close, rounding is done away from 0 (so.
for example, round(0.5) is 1.0 and round(-0.5) is -1.0).
Example:
a = [0.05 + x/10.0 for x in range(10)]
b str(round(x, 1))
for x in a: print x,
print
for x in a: print str(round(x, 1)) + " ",

Gives:
0.05 0.15 0.25 0.35 0.45 0.55 0.65 0.75 0.85 0.95
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

But to avoid a bias toward rounding up there is another way do this:
If the digit immediately to the right of the last sig. fig. is more
than 5, you round up.
If the digit immediately to the right of the last sig. fig. is less
than 5, you round down.
If the digit immediately to the right of the last sig. fig. is equal to
5, you round up if the last sig. fig. is odd. You round down if the
last sig. fig. is even. You round up if 5 is followed by nonzero
digits, regardless of whether the last sig. fig. is odd or even.
http://www.towson.edu/~ladon/roundo~1.html
http://mathforum.org/library/drmath/view/58972.html
http://mathforum.org/library/drmath/view/58961.html
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.
7) Maybe it can be useful to extended the reload(module) semantic:
reload(module [, recurse=False])
If recurse=True it reloads the module and recursively all the modules
that it imports.
8) Why reload is a function and import is a statement? (So why isn't
reload a statement too, or both functions?)
9) Functions without a return statement return None:
def foo(x): print x
I think the compiler/interpreter can give a "compilatio n warning" where
such function results are assigned to something:
y = foo(x)
(I know that some of such cases cannot be spotted at compilation time,
but the other cases can be useful too).
I don't know if PyChecker does this already. Generally speaking I'd
like to see some of those checks built into the normal interpreter.
Instructions like:
open = "hello"
Are legal, but maybe a "compilatio n warning" can be useful here too
(and maybe even a runtime warning if a Verbose flag is set).
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.)
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

Beside the global.y, maybe it can exist a syntax like upper.y or
caller.y that means the name y in the upper context. upper.upper.y etc.
12) Mathematica's interactive IDE suggests possible spelling errors;
this feature is often useful, works with builtin name functions too,
and it can be switched off.
In[1]:= sin2 = N[Sin[2]]
Out[1]= 0.909297

In[2]:= sina2
General::"spell 1": "Possible spelling error: new symbol name "sina2"
is similar to existing symbol "sin2".
Out[2]= sina2

I don't know if some Python IDEs (or IPython) do this already, but it
can be useful in Pythonwin.
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...
------------------

14) In one of my last emails of notes, I've tried to explain the
Pattern Matching programming paradigm of Mathematica. Josiah Carlson
answered me:

http://groups-beta.google.com/group/...5600094cb281c1
In the C/C++ world, that is called polymorphism.
You can do polymorphism with Python, and decorators may make it

easier...

This kind of programming is like the use of a kind regular expression
on the parameters of functions. Here are some fast operators, from the
(copyrighted) online help:

_ or Blank[ ] is a pattern object that can stand for any Mathematica
expression.
For example this info comes from:
http://documents.wolfram.com/mathema...unctions/Blank
This is used for example in the definition of functions:
f[x_] := x^2

__ (two _ characters) or BlankSequence[ ] is a pattern object that can
stand for any sequence of one or more Mathematica expressions.

___ (three _ characters) or BlankNullSequen ce[ ] is a pattern object
that can stand for any sequence of zero or more Mathematica
expressions.
___h or BlankNullSequen ce[h] can stand for any sequence of expressions,
all of which have head h.

p1 | p2 | ... is a pattern object which represents any of the patterns
pi

s:obj represents the pattern object obj, assigned the name s. When a
transformation rule is used, any occurrence of s on the right*hand
side is replaced by whatever expression it matched on the left*hand
side. The operator : has a comparatively low precedence. The expression
x:_+_ is thus interpreted as x:(_+_), not (x:_)+_.

p:v is a pattern object which represents an expression of the form p,
which, if omitted, should be replaced by v. Optional is used to specify
"optional arguments" in functions represented by patterns. The pattern
object p gives the form the argument should have, if it is present. The
expression v gives the "default value" to use if the argument is
absent. Example: the pattern f[x_, y_:1] is matched by f[a], with x
taking the value a, and y taking the value 1. It can also be matched by
f[a, b], with y taking the value b.

p.. is a pattern object which represents a sequence of one or more
expressions, each matching p.

p... is a pattern object which represents a sequence of zero or more
expressions, each matching p.

patt /; test is a pattern which matches only if the evaluation of test
yields True.
Example: f[x_] := fp[x] /; x > 1 defines a function in the case when a.
lhs := Module[{vars}, rhs /; test] allows local variables to be shared
between test and rhs. You can use the same construction with Block and
With.

p?test is a pattern object that stands for any expression which matches
p, and on which the application of test gives True. Ex:
p1[x_?NumberQ] := Sqrt[x]
p2[x_?NumericQ] := Sqr[x]

Verbatim[expr] represents expr in pattern matching, requiring that expr
be matched exactly as it appears, with no substitutions for blanks or
other transformations . Verbatim[x_] will match only the actual
expression x_. Verbatim is useful in setting up rules for transforming
other transformation rules.

HoldPattern[expr] is equivalent to expr for pattern matching, but
maintains expr in an unevaluated form.

Orderless is an attribute that can be assigned to a symbol f to
indicate that the elements a in expressions of the form f[e1, e2, ...]
should automatically be sorted into canonical order. This property is
accounted for in pattern matching.

Flat is an attribute that can be assigned to a symbol f to indicate
that all expressions involving nested functions f should be flattened
out. This property is accounted for in pattern matching.

OneIdentity is an attribute that can be assigned to a symbol f to
indicate that f[x], f[f[x]], etc. are all equivalent to x for the
purpose of pattern matching.

Default[f], if defined, gives the default value for arguments of the
function f obtained with a _. pattern object.
Default[f, i] gives the default value to use when _. appears as the
i-th argument of f.

Cases[{e1, e2, ...}, pattern] gives a list of the a that match the
pattern.
Cases[{e1, e2, ...}, pattern -> rhs] gives a list of the values of rhs
corresponding to the ei that match the pattern.

Position[expr, pattern] gives a list of the positions at which objects
matching pattern appear in expr.

Select[list, crit] picks out all elements a of list for which crit[ei]
is True.

DeleteCases[expr, pattern] removes all elements of expr which match
pattern.
DeleteCases[expr, pattern, levspec] removes all parts of expr on levels
specified by levspec which match pattern.
Example : DeleteCases[{1, a, 2, b}, _Integer] ==> {a, b}

Count[list, pattern] gives the number of elements in list that match
pattern.

MatchQ[expr, form] returns True if the pattern form matches expr, and
returns False otherwise.

It may look strange, but an expert can even use it to write small full
programs... But usually they are used just when necessary.
Note that I'm not suggesting to add those (all) into python.

------------------

15) NetLogo is a kind of logo derived from StarLogo, implemented in
Java.
http://ccl.northwestern.edu/netlogo/
I think it contains some ideas that can be useful for Python too.
- It has built-in some hi-level data structures, like the usual turtle
(but usually you use LOTS of turtles at the same time, in parallel),
and the patch (programmable cellular automata layers, each cell can be
programmed and it can interact with nearby cells or nearby turtles)
- It contains built-in graphics, because it's often useful for people
that starts to program, and it's useful for lots of other things. In
Python it can be useful a tiny and easy "fast" graphics library
(Tkinter too can be used, but something simpler can be useful for some
quick&dirty graphics. Maybe this library can also be faster than the
Tkinter pixel plotting and the pixel matrix visualisation).
- It contains few types of built-in graphs to plot variables, etc. (for
python there are many external plotters).
- Its built-in widgets are really easy to use (they are defined inside
NetLogo and StarLogo source), but they probably look too much toy-like
for Python programs...
- This language contains lots of other nice ideas. Some of them
probably look too much toy-like, still some examples:
http://ccl.northwestern.edu/netlogo/models/
Show that this language is only partially a toy, and it can be useful
to understand and learn nonlinear dynamics of many systems.

This is a source, usually some parts of it (like widget positioning and
parameters) are managed by the IDE:
http://ccl.northwestern.edu/netlogo/...logy/Fur.nlogo
Bye,
bear hugs,
Bearophile

Jul 18 '05 #1
22 1939
be************@ lycos.com wrote:
for i in 1..12: pass
for c in "a".."z": pass ..... @infix
def interval(x, y): return range(x, y+1) # 2 parameters needed
assert 5 interval 9 == interval(5,9) ..... 10) There can be something in the middle between the def statement and
the lambda.
These will likely not appear in CPython standard, but Livelogix runs on
top of the CPython VM and supports ".." sequences and custom infix
operators: http://logix.livelogix.com/tutorial/...ard-Logix.html
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

Beside the global.y, maybe it can exist a syntax like upper.y or
caller.y that means the name y in the upper context. upper.upper.y etc.
This will also likely never appear in Python. I like your idea though.
I implemented the same exact thing a couple months ago. One
difference though, you only need to type out the full "global.y" if you
want to differentiate it from a local variable with the same name.

15) NetLogo is a kind of logo derived from StarLogo, implemented in
Java.
Show that this language is only partially a toy, and it can be useful
to understand and learn nonlinear dynamics of many systems.


If you want to do something like Netlogo but using Python instead of
Logo, see: http://repast.sourceforge.net/
You can script repast in jython or you can script repast.net.

Also, you might request the NetLogo and StarLogo developers to support
Jython (in addition to Logo) scripting in their next version (which is
already in development and supports 3D).
Jul 18 '05 #2
bearophileHUGS:
[on Python's O(n) list insertion/deletion) at any place other than tail
(My hypothesis: to keep list implementation a bit simpler, to avoid
wasting memory for the head buffer, and to keep them a little faster,
avoiding the use of the skip index S).
Add its relative infrequent need.
2) I usually prefer explicit verbose syntax, instead of cryptic symbols
(like decorator syntax), but I like the infix Pascal syntax ".." to
specify a closed interval (a tuple?) of integers or letters assert 1..9 == tuple(range(1,1 0))
for i in 1..12: pass
for c in "a".."z": pass
It's been proposed several times. I thought there was a PEP
but I can't find it. One problem with it; what does

for x in 1 .. "a":

do? (BTW, it needs to be 1 .. 12 not 1..12 because 1. will be
interpreted as the floating point value "1.0".)

What does
a = MyClass()
b = AnotherClass()
for x in a .. b:
print x

do? That is, what's the generic protocol? In Pascal it
works because you also specify the type and Pascal has
an incr while Python doesn't.
3) I think it can be useful a way to define infix functions, like this
imaginary decorator syntax:

@infix
def interval(x, y): return range(x, y+1) # 2 parameters needed

This may allow:
assert 5 interval 9 == interval(5,9)
Maybe you could give an example of when you need this in
real life?

What does

1 + 2 * 3 interval 9 / 3 - 7

do? That is, what's the precedence? Does this only work
for binary or is there a way to allow unary or other
n-ary (including 0-ary?) functions?

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 ^_^),
But to someone with C experience or any language which derives
its formatting string from C, Python's is easier to understand than
your Pascal one.

A Python view is that there should be only one obvious way to do
a task. Supporting both C and Pascal style format strings breaks
that. Then again, having both the old % and the new PEP 292 string
templates means there's already two different ways to do string
formatting.
For example this Delphi program: ... const a = -12345.67890; ... writeln(a:4:20) ; ... Gives: ... -12345.678900000 00000000000 Python says that's
"%.20f" % -12345.67890 '-12345.678900000 00079453457'

I don't think Pascal is IEEE enough.

note also that the Pascal-style formatting strings are less
capable than Python's, though few people use features like
"% 2.3f" % -12.34 '-12.340' "% 2.3f" % 12.34 ' 12.340'

5) From the docs about round: ... But to avoid a bias toward rounding up there is another way do this:
There are even more ways than that. See
http://www.python.org/peps/pep-0327....ing-algorithms

The solution chosen was not to change 'round' but to provide
a new data type -- Decimal. This is in Python 2.4.
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. .. I think this semantic can be extended:
A real-life example would also be helpful here.

What does
map(len, "Blah", level = 200)
return?

In general, most people prefer to not use map and instead
use list comprehensions and (with 2.4) generator comprehensions.

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).
You need to learn more about the Pythonic way of thinking
of things. The usual solution for this is to have "level = None".
7) Maybe it can be useful to extended the reload(module) semantic:
reload(module [, recurse=False])
If recurse=True it reloads the module and recursively all the modules
that it imports.
It isn't that simple. Reloading modules isn't sufficient.
Consider

import spam
a = spam.Eggs()
reload(spam)
print isinstance(a, spam.Eggs)

This will print False because a contains a reference to
the old Eggs which contains a reference to the old spam module.

As I recall, Twisted has some super-reload code that may be
what you want. See
http://twistedmatrix.com/documents/c...n.rebuild.html
8) Why reload is a function and import is a statement? (So why isn't
reload a statement too, or both functions?)
import uses the underlying __import__ function.

Consider using the __import__ function directly

math = __import__("mat h")

The double use of the name "math" is annoying and error prone.

It's more complicated with module hierarchies.
xml = __import__("xml .sax.handler")
xml.sax.handler <module 'xml.sax.handle r' from '/sw/lib/python2.3/xml/sax/handler.pyc'> xml.sax.saxutil s Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'saxutils' __import__("xml .sax.saxutils") <module 'xml' from '/sw/lib/python2.3/xml/__init__.pyc'> xml.sax.saxutil s <module 'xml.sax.saxuti ls' from '/sw/lib/python2.3/xml/sax/saxutils.pyc'>

Reload takes a reference to the module to reload. Consider
import UserString
x = UserString
reload(x)

This reloads UserString. It could be a statement but there's
no advantage to doing so.

9) Functions without a return statement return None: def foo(x): print x
I think the compiler/interpreter can give a "compilatio n warning" where
such function results are assigned to something: y = foo(x)
You might think so but you'ld be wrong.

Consider

def vikings():
pass

def f():
global vikings
def vikings():
print "Spammity spam!"
return 1.0

if random.random() > 0.5:
f()

x = vikings()

More realistically I've done

class DebugCall:
def __init__(self, obj):
self.__obj = obj
def __call__(self, *args, **kwargs):
print "Calling", self.__obj, "with", args, kwargs
x = self.__obj(*arg s, **kwargs)
print "Returned with", x
return x

from wherever import f
f = DebugCall(f)

I don't want it generating a warning for those cases where
the implicit None is returned.

A more useful (IMHO) is to have PyChecker check for cases
where both explicit and implicit returns can occur in the
same function. Don't know if it does that already.

Why haven't you looked at PyChecker to see what it does?
(I know that some of such cases cannot be spotted at compilation time,
but the other cases can be useful too). I don't know if PyChecker does
this already. Generally speaking I'd like to see some of those checks
built into the normal interpreter. Instructions like:
open = "hello"
Are legal, but maybe a "compilatio n warning" can be useful here too (and
maybe even a runtime warning if a Verbose flag is set).
There's a PEP for that. See
http://www.python.org/peps/pep-0329.html

Given your questions it would be appropriate for you to read all the
PEPs.
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.)
It's been talked about. Problems are:
- how is it written?
- how big is the region between a lambda an a def?

Try giving real world examples of when you would
use this 'fun' and compare it to lambda and def forms.
you'll find there's at most one extra line of code
needed. That doesn't seem worthwhile.

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

Beside the global.y, maybe it can exist a syntax like upper.y or
caller.y that means the name y in the upper context. upper.upper.y etc.
It does have the advantage of being explicit rather than
implicit.
12) Mathematica's interactive IDE suggests possible spelling errors;
I don't know anything about the IDEs. I have enabled the
tab-complete in my interactive Python session which is nice.

I believe IPython is the closest to giving a Mathematica-like feel.
There's also tmPython.
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...
Could you explain why it might be useful?
14) In one of my last emails of notes, I've tried to explain the Pattern
Matching programming paradigm of Mathematica. Josiah Carlson answered
me:
Was there a question in all that?

You are proposing Python include a Prolog-style (or
CLIPS or Linda or ..) programming idiom, yes? Could you
also suggest a case when one would use it?
15) NetLogo is a kind of logo derived from StarLogo, implemented in
Java.
How about the "turtle" standard library?

I must say it's getting pretty annoying to say things like
"when would this be useful?" and "have you read the documentation?"
for your statements.
Maybe this library can also be faster than the Tkinter pixel
plotting and the pixel matrix visualisation).


See also matplotlib, chaco, and other libraries that work hard
to make this simple. Have you done any research on what Python
can do or do you think ... no, sorry, I'm getting snippy.

Andrew
da***@dalkescie ntific.com

Jul 18 '05 #3
Andrew Dalke <da***@dalkesci entific.com> writes:
What does
a = MyClass()
b = AnotherClass()
for x in a .. b:
print x

do? That is, what's the generic protocol?


".." just becomes an operator like + or whatever, which you can define
in your class definition:

class MyClass:
def __dotdot__(self , other):
return xrange(self.wha tsit(), other.whatsit() )

The .. operation is required to return an iterator.
Jul 18 '05 #4
be************@ 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:

{$APPTYPE CONSOLE}
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.2345678900000 0E+0004
-12346
-12345.68
-12345.678900000 00000000000
-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>.<deci mals>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
situation.)
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)

or

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_express ion()

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:

http://www.python.org/peps/pep-0312.html

which suggests a simpler syntax for this kind of usage.
Steve
Jul 18 '05 #5
Andrew Dalke wrote:
I must say it's getting pretty annoying to say things like
"when would this be useful?" and "have you read the documentation?"
for your statements.


I'll second that. Please, "Bearophile ", do us the courtesy of checking

(1) Google groups archive of the mailing list:
http://groups-beta.google.com/group/comp.lang.python

and

(2) The Python Enhancement Proposals:
http://www.python.org/peps/

before posting another such set of questions. While most of the people
on this list are nice enough to answer your questions anyway, the
answers are already out there for at least half of your questions, if
you would do us the courtesy of checking first.

Thanks!

Steve
Jul 18 '05 #6
Paul Rubin wrote:
".." just becomes an operator like + or whatever, which you can define
in your class definition:

class MyClass:
def __dotdot__(self , other):
return xrange(self.wha tsit(), other.whatsit() )

The .. operation is required to return an iterator.


Ahh, I see.

This should be put into a PEP. Some obvious questions:
- ".." or "..." ? The latter is already available for
use in slices

- If "..." should the name be "__ellipsis __"? If ".."
should the name be "__range___ "?

- Should range(x, y) first attempt x.__range__(y)?

- Can it be used outside of a for statement? Ie, does
x = "a" ... "b"
return a generic iterator? Almost certainly as that
fits in nicely with the existing 'for' syntax.

- What's the precedence? Given
x = a .. b .. c
x = 1 + 2 .. 5 * 3
x = 1 ** 5 .. 4 ** 2
etc., what is meant? Most likely .. should have the
lowest precedence, evaluated left to right.

- is there an "__rdotdot_ _"?

- any way to specify "use the normal beginning"? Like
for x in .. 5: # same as 0 .. 5
-or (the oft rejected)-
for x in 5:

- any way to specify "ad infinitum"? Like
for x in 0 .. Infinity:
-or-
for x in 0 ... :

- does "for x in 10 .. 0" the same as xrange(10,0) (what
you propose) or xrange(10, 0, -1)?

- do strings work for len(s) > 1? Eg, "A000" .. "A999"?

- What do you think of (assuming the use of "...")
for x in 0.....100:?

- What's the advantage of ".." over, say a function or
method? That is, why does the new binary operator
prove so useful? In one of my gedanken experiments
I considered getting the 10th through 20th prime
for x in primes(10) .. primes(20):
but that seems clumsier than
for x in primes_between( 10, 20):
in part because it requires "primes(10) " to have some
meaning. I suppose
for x in primes(10) .. 20:
could also work though that should in my mind generate
primes up to the number 20 and not the 20th prime.
Note that primes(10) cannot return the 10th prime as
the value 29.

Andrew
da***@dalkescie ntific.com

Jul 18 '05 #7
be************@ lycos.com writes:
@infix
def interval(x, y): return range(x, y+1) # 2 parameters needed

This may allow:
assert 5 interval 9 == interval(5,9)


I don't like the idea of turning words into operators. I'd much rather
see something like:

@infix('..')
def interval(x, y):
return range(x, y + 1)

assert 5 .. 9 == interval(5, 10)

This would also allow us to start working on doing away with the magic
method names for current operators, which I think would be an
improvement.

As others have pointed out, you do need to do something about operator
precedence. For existing operators, that's easy - they keep their
precedence. For new operators, it's harder.

You also need to worry about binding order. At the very least, you
can specify that all new operators bind left to right. But that might
not be what you want.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #8
Mike Meyer wrote:
be************@ lycos.com writes:

@infix
def interval(x, y): return range(x, y+1) # 2 parameters needed

This may allow:
assert 5 interval 9 == interval(5,9)

I don't like the idea of turning words into operators. I'd much rather
see something like:

@infix('..')
def interval(x, y):
return range(x, y + 1)

assert 5 .. 9 == interval(5, 10)

This would also allow us to start working on doing away with the magic
method names for current operators, which I think would be an
improvement.

Well, perhaps you can explain how a change that's made at run time
(calling the decorator) can affect the parser's compile time behavior,
then. At the moment, IIRC, the only way Python code can affect the
parser's behavior is in the __future__ module, which must be imported at
the very head of a module.
As others have pointed out, you do need to do something about operator
precedence. For existing operators, that's easy - they keep their
precedence. For new operators, it's harder.
Clearly you'd need some mechanism to specify preference, either
relatively or absolutely. I seem to remember Algol 68 allowed this.
You also need to worry about binding order. At the very least, you
can specify that all new operators bind left to right. But that might
not be what you want.

Associativity and precedence will also have to affect the parsing of the
code, of course. Overall this would be a very ambitious change.

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 94 3119
Jul 18 '05 #9
Steve Holden <st***@holdenwe b.com> writes:
Mike Meyer wrote:
be************@ lycos.com writes:
@infix
def interval(x, y): return range(x, y+1) # 2 parameters needed

This may allow:
assert 5 interval 9 == interval(5,9)

I don't like the idea of turning words into operators. I'd much
rather
see something like:
@infix('..')
def interval(x, y):
return range(x, y + 1)
assert 5 .. 9 == interval(5, 10)
This would also allow us to start working on doing away with the
magic
method names for current operators, which I think would be an
improvement.

Well, perhaps you can explain how a change that's made at run time
(calling the decorator) can affect the parser's compile time behavior,
then. At the moment, IIRC, the only way Python code can affect the
parser's behavior is in the __future__ module, which must be imported
at the very head of a module.


By modifying the parsers grammer at runtime. After all, it's just a
data structure that's internal to the compiler.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5020
by: NickBlooruk | last post by:
Hello, I have successfully linked a Lotus Notes server to our SQL Server database using an ODBC connection. This works fine when wanting to select records eg openquery(LOTUSNOTES2, 'select * from Person' ) The problem I have is when I try to update the record I get an error eg update openquery(LOTUSNOTES, 'select * from Person where
3
1807
by: Jim Cobban | last post by:
I have a set of web pages that are organized in pairs. One of each pair contains a graphic and the other is an extended description of the graphic. I am trying to set it up that selecting a single hyperlink causes the correct pair of web pages to be loaded into two windows. My current approach is to specify an onload action in the <BODY> tag of one of the pages that requests that the corresponding description page be loaded into the...
9
2040
by: bojan.pikl | last post by:
Hi, I am making a simple calendar. I would like to have the ability of notes. I was thinking of using this kind of notes.xml file. I am not sure how to operate with nodes. I just need to access something like SelectNode("/NOTES/y"+Year+"/m"+Month+"/d"+Day); (I'd get /NOTES/y2006/m5/d18) and than I'd read what is in there. Can anyone help please? <NOTES> <y2006> <m4> <DAYS>1,2,13</DAYS>
2
11844
by: charliej2001 | last post by:
Hi all I'm trying to set up an Access database that will be able to import contact information from the lotus notes 6.5 Address book. The idea is that the code runs from access to import all of the contacts, using COM. I've written the following vba code so far to import the contacts
1
13048
by: Joe | last post by:
HI Has anyone been able to work with lotus notes automation classes??? Can you post sample code of how to use these classes. I have setup in VB but I am not able to port to C# This is what I have so far - I cannot create a session and not sure how to setup From/Subject
4
9987
by: navyliu | last post by:
I want send Lotus Notes Email automatic in my Application.I googled this topic but I can't find the solution. Does anyone have ever use this function?Can you give me some advice? Thanks a lot!
4
9716
by: Bob Alston | last post by:
I am trying to access data in a Lotus Notes database, from Access. The Notes database is release R5. I installed NotesSQL 8.x and have it working. However it chokes on one of the tables and references problems with the dates. If I create a query, without these dozen or so dates, I can access all the data. However I am guessing that most of the dates are correct and only some are bad.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.