473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

function with a state

is it possible in Python to create a function that maintains a variable
value?

something like this:

globe=0;
def myFun():
globe=globe+1
return globe

apparently it can't be done like that. I thought it can probably be
done by prefixing the variable with some package context...

the Python doc is quite stilted. Where in the python doc is a programer
supposed read about how the package/module system in Python works?
(besides the tutorial that touches it)

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
40 2919
Xah Lee wrote:
globe=0;
def myFun():
globe=globe+1
return globe


The short answer is to use the global statement:

globe=0
def myFun():
global globe
globe=globe+1
return globe

more elegant is:

globe=0
globe=myfun(globe)
def myFun(var):
return var+1

and still more elegant is using classes and class attributes instead of
global variables.

-pu
Jul 18 '05 #2
Xah Lee <xa*@xahlee.org> wrote:
is it possible in Python to create a function that maintains a
variable value?
Yes. There's no concept of a 'static' function variable as such, but
there are many other ways to achieve the same thing.
globe=0;
def myFun():
globe=globe+1
return globe


This would work except that you have to tell it explicitly that you're
working with a global, otherwise Python sees the "globe=" and decides
you want 'globe' be a local variable.

globe= 0
def myFun():
global globe
globe= globe+1
return globe

Alternatively, wrap the value in a mutable type so you don't have to do
an assignment (and can use it in nested scopes):

globe= [ 0 ]
def myFun():
globe[0]+= 1
return globe[0]

A hack you can use to hide statics from code outside the function is to
abuse the fact that default parameters are calcuated at define-time:

def myFun(globe= [ 0 ]):
globe[0]+= 1
return globe[0]

For more complicated cases, it might be better to be explicit and use
objects:

class Counter:
def __init__(self):
self.globe= 0
def count(self):
self.globe+= 1
return self.globe

myFun= Counter().count

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/

Jul 18 '05 #3
Patrick Useldinger wrote:
The short answer is to use the global statement:

globe=0
def myFun():
global globe
globe=globe+1
return globe

more elegant is:

globe=0
globe=myfun(globe)
def myFun(var):
return var+1


This mystifies me. What is myfun()? What is var intended to be?

Kent
Jul 18 '05 #4
Xah Lee wrote:
is it possible in Python to create a function that maintains a variable
value?

something like this:

globe=0;
def myFun():
globe=globe+1
return globe


You could work with function attributes:
def myFun():
try: myFun.globe += 1
except: myFun.globe = 1

return myFun.globe

or with a default function argument:
class Dummy: pass

def myFun(globe=Dummy()):
try: globe.globe += 1
except: globe.globe = 1

return globe.globe

Reinhold
Jul 18 '05 #6
"Xah Lee" <xa*@xahlee.org> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
globe=0;
def myFun():
globe=globe+1
return globe

apparently it can't be done like that. I thought it can probably be
done by prefixing the variable with some package context...


You can do this:

globe=0
def myFun():
global globe
globe=globe+1
return globe

The question you should ask yourself, however, is why you want to do this.
Do you really want to tie your function to a single global variable? Are
you sure you will never need more than one?

For example, the function you've written represents a counter. Are you sure
you will never need more than one such counter?
Jul 18 '05 #7
I believe python docs are quite *un*-stilted. Modules and packages are
not complicated. Read chapter 7 of the nutshell, it's only 10 pages
long. 2.3 and 2.4 didn't introduce any fundamental changes in how
modules work AFAIK

Jul 18 '05 #8
Kent Johnson wrote:
globe=0
globe=myfun(globe)
def myFun(var):
return var+1

This mystifies me. What is myfun()? What is var intended to be?


myfun is an error ;-) should be myFun, of course.

var is parameter of function myFun. If you call myFun with variable
globe, all references to var will be replaced by globe inside function
myFun.

-pu
Jul 18 '05 #9
Patrick Useldinger wrote:
Kent Johnson wrote:
globe=0
globe=myfun(globe)
def myFun(var):
return var+1


This mystifies me. What is myfun()? What is var intended to be?

myfun is an error ;-) should be myFun, of course.

var is parameter of function myFun. If you call myFun with variable
globe, all references to var will be replaced by globe inside function
myFun.


Oh. I thought there was some deep magic here that I was missing :-)

You also have to define myFun before you call it...

def myFun(var):
return var+1

globe = 0
....
globe = myFun(globe)

Kent
Jul 18 '05 #10
"Xah Lee" <xa*@xahlee.org> writes:
the Python doc is quite stilted. Where in the python doc is a programer
supposed read about how the package/module system in Python works?
(besides the tutorial that touches it)


The python docs at <URL: http://docs.python.org/ref/naming.html > are
perfectly clear. It explains the namespaces used when resolving
variable names, and how to subvert the standard resolution to solve
your problem.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #11
On Sun, 06 Mar 2005 09:44:41 +0100, Patrick Useldinger
<pu*********@gmail.com> wrote:
Xah Lee wrote:
globe=0;
def myFun():
globe=globe+1
return globe


The short answer is to use the global statement:

globe=0
def myFun():
global globe
globe=globe+1
return globe

more elegant is:

globe=0
globe=myfun(globe)
def myFun(var):
return var+1

and still more elegant is using classes and class attributes instead of
global variables.


Or what about just using the function object directly?

def myFun():
myFun.x += 1
return myFun.x
myFun.x = 0

for test in range(10):
assert myFun()+1 == myFun()
assert myFun()*2+3 == myFun()+myFun()
assert range(myFun(), myFun()+9) == [myFun() for x in range(10)]
assert range(myFun()+2, myFun()+11) == [myFun() for x in range(10)]

:))

couldn't-help-feeding-the-troll-ly-y'rs.
Stephen Thorne.
Jul 18 '05 #12
Reinhold Birkenfeld wrote:
or with a default function argument:
class Dummy: pass

def myFun(globe=Dummy()):
try: globe.globe += 1
except: globe.globe = 1

return globe.globe


A quicker way:

def myFun(globe=[0]):
globe[0] += 1
return globe[0]

Reinhold
Jul 18 '05 #13
thanks for the help...

-------
the python doc is stilted. It tried to organized the thing and with a
style around some highbrow inane "computer science" outlook.

i found the little section on global
(http://python.org/doc/2.4/ref/global.html)
and can't make out what shit it is trying to say without having read
and figured out the entire doc of its style and contexts and
definitions. (formalization varies and computing model and jargons mean
different things.)

Python doc writers needs to re-organize and re-style their docs so that
its organization is towards programing, as opposed to how the
implementation works (as in the Lib Reference), or a formalization of
the language spec. (e.g. the fucking semi-joke of "(for language
lawyers)" and BNF and those Runtime "Service" shits.) Its style
needs to shift from highbrowism to pragmatic and exemplary.

I've addressed some of the jargon-riding ills common in industry with
examples from the Python doc, archived here:
http://xahlee.org/Periodic_dosage_di...ami_cukta.html

as to the way of its stiltedenss and academicism, which make it hard
for programers to find or apply any info, i'll expound later.

PS just so that there is no misunderstanding: The docs of unix and
Perl, are fucking criminally incompetent. Python docs, although stilted
in a academic way, but nevertheless is solid, and its writers are
educated, and tried best to make it a quality one, albeit sometimes
inevitably showed some masterbation and jargonization. While the unix
and Perl docs, (and essentially all things out of unix, e.g. Apache
docs), are fucking incompetent drivels and in many cases exorbitant
lies, and they semi-present it as humor and want and brainwash people
to take them as norm. In a nutshell, these people are spreading
untruths and indirectly are causing massive harm in the computing
industry. People, we need to stop it. This each of us can do by not
accepting their attitudes or behavior. In online forums, work place,
conventions, conversations etc., raise questions or otherwise voice
your opinion whenever you can.

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #14
On 8 Mar 2005 17:07:31 -0800, Xah Lee <xa*@xahlee.org> wrote:
thanks for the help...

-------
the python doc is stilted. It tried to organized the thing and with a
style around some highbrow inane "computer science" outlook.

i found the little section on global
(http://python.org/doc/2.4/ref/global.html)
and can't make out what shit it is trying to say without having read
and figured out the entire doc of its style and contexts and
definitions. (formalization varies and computing model and jargons mean
different things.)

Python doc writers needs to re-organize and re-style their docs so that
its organization is towards programing, as opposed to how the
implementation works (as in the Lib Reference), or a formalization of
the language spec. (e.g. the fucking semi-joke of "(for language
lawyers)" and BNF and those Runtime "Service" shits.) Its style
needs to shift from highbrowism to pragmatic and exemplary.

I've addressed some of the jargon-riding ills common in industry with
examples from the Python doc, archived here:
http://xahlee.org/Periodic_dosage_di...ami_cukta.html

as to the way of its stiltedenss and academicism, which make it hard
for programers to find or apply any info, i'll expound later.

PS just so that there is no misunderstanding: The docs of unix and
Perl, are fucking criminally incompetent. Python docs, although stilted
in a academic way, but nevertheless is solid, and its writers are
educated, and tried best to make it a quality one, albeit sometimes
inevitably showed some masterbation and jargonization. While the unix
and Perl docs, (and essentially all things out of unix, e.g. Apache
docs), are fucking incompetent drivels and in many cases exorbitant
lies, and they semi-present it as humor and want and brainwash people
to take them as norm. In a nutshell, these people are spreading
untruths and indirectly are causing massive harm in the computing
industry. People, we need to stop it. This each of us can do by not
accepting their attitudes or behavior. In online forums, work place,
conventions, conversations etc., raise questions or otherwise voice
your opinion whenever you can.

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

--
http://mail.python.org/mailman/listinfo/python-list


Have you submitted a patch? I'm curious how you would document "global".

--
Sean Blakey
Saint of Mild Amusement, Evil Genius, Big Geek
Python/Java/C++/C(Unix/Windows/Palm/Web) developer
quine = ['print "quine =",quine,"; exec(quine[0])"'] ; exec(quine[0])
Jul 18 '05 #15
Xah Lee wrote:
thanks for the help...

-------
the python doc is stilted. It tried to organized the thing and with a
style around some highbrow inane "computer science" outlook.

[usual semi-literate expletive-deleted rant omitted]
<sarcasm>And your writing is so lucid and comprehensible, it's a pity
you can't simply rewrite the documentation so the rest of the world can
understand it.</sarcasm>

While the Python documentation does, of course, have its shortcomings,
you will not improve it by indulging in this kind of nonsense, which
appears to have become some kind of reflex. Your apparent assumption
that your idiotic behavior will make any difference to anything or
anybody is arrogant beyond belief. This is a real pity, as your web site
makes it apparent that in matters where you possess genuine knowledge
you can produce worthwhile work.

Please abandon any notion that your own writings are in any way superior
to the object of your complaints. At least I can learn Python from the
documentation. From you I can learn nothing, you are so self-absorbed
that it would take a pneumatic drill to penetrate that thick skull.

regards
Steve

PS: There is no "e" in masturbation, as any properly-qualified wanker
should know.

Jul 18 '05 #16
>def myFun(var):
return var+1
globe = 0
globe = myFun(globe)


this is intriguing. How does it work?
not a rhetorical question, but where in the python doc can i read about
it?

thanks.

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #17
Xah Lee wrote:
def myFun(var):
return var+1
globe = 0
globe = myFun(globe)


this is intriguing. How does it work?
not a rhetorical question, but where in the python doc can i read about
it?


The tutorial, presumably, since there is nothing here
that isn't covered by the most basic aspects of Python.

Really, what did you think was there that was so
intriguing? A function is defined. It takes a
parameter and returns its value plus one. A
variable is created with the value 0. The
function is called, passing the value of the
variable, and the return value (remember, the
function just adds one!) is assigned to the
variable. Now "globe" equals 1.

Again, what aspect of this startled you?

-Peter
Jul 18 '05 #18
Nevermind. I was thinking too much. :) Thanks.

Xah
Peter Hansen wrote:
Xah Lee wrote:
def myFun(var):
return var+1
globe = 0
globe = myFun(globe)


this is intriguing. How does it work?
not a rhetorical question, but where in the python doc can i read about it?


The tutorial, presumably, since there is nothing here
that isn't covered by the most basic aspects of Python.

Really, what did you think was there that was so
intriguing? A function is defined. It takes a
parameter and returns its value plus one. A
variable is created with the value 0. The
function is called, passing the value of the
variable, and the return value (remember, the
function just adds one!) is assigned to the
variable. Now "globe" equals 1.

Again, what aspect of this startled you?

-Peter


Jul 18 '05 #19
an********@doxdesk.com wrote:
Xah Lee <xa*@xahlee.org> wrote:
is it possible in Python to create a function that maintains a
variable value?


Yes. There's no concept of a 'static' function variable as such, but
there are many other ways to achieve the same thing.
globe=0;
def myFun():
globe=globe+1
return globe


[snip]

For more complicated cases, it might be better to be explicit and use
objects:

class Counter:
def __init__(self):
self.globe= 0
def count(self):
self.globe+= 1
return self.globe

myFun= Counter().count


You can also use generators.
def myGenerator(): .... var = 0
.... while True:
.... var += 1
.... yield var
.... myFun = myGenerator().next
myFun() 1 myFun() 2 myFun() 3 myFun()

4

Jul 18 '05 #20
You don't understand the "global" statement in Python, but you do
understand Software industry in general? Smart...
Jul 18 '05 #21
The Python doc is relatively lousy, from content organization to the
tech writing quality.

I think i'll just post snippets of my comments as i find them. (and
feel like exposing)

Python doc:
http://python.org/doc/2.4/lib/comparisons.html

Quote:
Comparison operations are supported by all objects. They all have the
same priority (which is higher than that of the Boolean operations).
Comparisons can be chained arbitrarily; for example, x < y <= z is
equivalent to x < y and y <= z, except that y is evaluated only once
(but in both cases z is not evaluated at all when x < y is found to be
false).

--
Problem: “Comparison operations are supported by all objects.”

This is very vague and ambiguous.

The word “object” has generic English meaning as well mighthave
very technical meaning in a language. In Python, it does not have very
pronounced technical meaning. For example, there's a chapter in Python
Library Ref titled “2. Built-In Objects”, and under it a section
“2.1 Built-in Functions”. Apparently, functions can't possibly be
meant as a “object” for comparisons.

Now suppose we take the object in the sentence to be sensible items as
numbers, lists etc. The clause “supported by all objects” is
ambiguous. What is meant by “supported”?

--
Problem: They all have the same priority (which is higher than that of
the Boolean operations).

This sentence is very stupid, in multitude of aspects.

The “priority” referred to here means operator precedence.

It tries to say that the comparison operator has higher syntactical
connectivity than boolean operators. E.g. “False and False==False”
means “False and (False==False)” and not “(False and
False)==False”.

However, the “they” pronoun from the context of previous sentence,
refers to “the comparison operation”, not “operator”. So, it
conjures the reader to think about some “operation precedence”,
which in itself cannot be ruled out as nonsense depending on the
context. Very fucking stupid confusional writing.

And, from pure writing aspect, the sentence “...(which is ...)” is
some kind of a juvenile latch on. If the author intent to make that
point, say it in its own sentence. e.g. The comparison operators have
higher precedence than boolean operators. It would be better to not
mention this at all. For practical considerations, very rare is the
case of mixing boolean and comparison operators, and if so, parenthesis
are likely used and is indeed a good practice. The proper place for
operator precedence is a table list all such, giving a clear view, and
in some appendix of language spec.

--
Problem: Comparisons can be chained arbitrarily; for example, x < y <=
z is equivalent to x < y and y <= z, except that y is evaluated only
once (but in both cases z is not evaluated at all when x < y is found
to be false).

Drop the word “arbitrarily”. It has no meaning here.

the whole sentence is one fucked up verbiage of pell-mell thinking and
writing. Here's one example of better:

Comparisons can be chained, and is evaluated from left to right. For
example, x < y <= z is equivalent to (x < y) <= z.

With respect to documentation style, it is questionable that this
aspect needs to be mentioned at all. In practice, if programers need to
chain comparisons, they will readily do so. This is not out of ordinary
in imperative languages, and evaluation from left to right is also not
extraordinary to cost a mention.

--
Problem: <> and != are alternate spellings for the same operator. != is
the preferred spelling; <> is obsolescent

Very bad choice of term “spellings” -- not a standard usagefor
computer language operators.

Better: “!=” can also be written as “<>”.

If “<>” is not likely to go out in future versions, don't even
mention about “preference”, because it has no effective meaning.
(if one wants to wax philosophical about “programing esthetics”, go
nag it outside of language documentation.)

In general, when something is obsolete or might go defunct in the
future, consider not even mentioning that construct. If necessary, add
it in a obscure place, and not adjacent to critical info. In many
places of Python documentation, this is breached.

--

This is just a quick partial analysis of one episode of incompetence i
see in Python docs in the past months i've had the pleasure to scan
here and there. A extreme pain in the ass.

I'm in fact somewhat surprised by this poor quality in writing. The
more egregious error is the hardware-oriented organization aka
technical drivel. But that i accept as common in imperative language
communities and in general the computing industry. But the poor quality
in the effectiveness and clarity of the writing itself surprised me. As
exhibited above, the writing is typical of programers, filled with
latch on sentences and unclear thinking. (they in general don't have a
clear picture of what they are talking about, and in cases they do,
they don't have the writing skills to express it effectively. (just as
a footnote: this writing problem isn't entirely the fault of programers
or Python doc writers. In part the English language (or in general
natural languages) are to blame, because they are exceptionally
illogical and really take years to master as a art by itself.))

The Python doc, though relatively incompetent, but the author have
tried the best. This is in contrast to documentations in unix related
things (unix tools, perl, apache, and so on etc), where the writers
have absolutely no sense of clear writing, and in most cases don't give
a damn and delight in drivel thinking of it as literary. A criminal of
this sort that does society huge damage is Larry Wall and the likes of
his cohorts in the unix community. (disclaimer: this is a piece of
opinion.)

addendum: quality writing takes time. Though, the critical part lies
not in the mastery of writing itself, but in clarity of thinking of
what exactly one wants to say. So, next time you are writing a tech
doc, first try to have a precise understanding of the object, and then
know exactly what is that you want to say about it, then the writing
will come out vastly better. If the precise understanding of the object
is not readily at hand (which is common and does not indicate
incompetence), being aware of it helps greatly in its exposition.

This and past critics on Python documentation and IT doc in general is
archived at
http://xahlee.org/Periodic_dosage_di...ami_cukta.html

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #22
Xah Lee wrote:
The Python doc is relatively lousy, from content organization to the
tech writing quality.


So write your own or fork the current one. I doubt if that's a problem.

Did you read Dive into Python?

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Jul 18 '05 #23
Xah Lee wrote:
The Python doc is relatively lousy, from content organization to the
tech writing quality.

I think i'll just post snippets of my comments as i find them. (and
feel like exposing)


The cross-posting idiot is back. Next he'll be posting his readings
from Richard Burton's _Arabian Nights_.

Xah, for you to think that Python is topical in the newsgroups to which
you posted, you must be sharing Karl Malbrain's cell at the loony bin.

Apologies to these various newsgroups, but I have no idea which one Xah
Lee actually reads or posts from. F'ups set.
Jul 18 '05 #24
Xah Lee wrote:
I think i'll just post snippets of my comments as i find them. (and
feel like exposing)
[ snipped ]


That is a very good analysis. Can you submit a documentation patch? I
would, but I'm too lazy to contribute. That doesn't mean I'm not
thankful for your efforts, though!

p

Jul 18 '05 #25
Paul L. Du Bois wrote:
Xah Lee wrote:
I think i'll just post...
[ snipped ]

That is a very good analysis. Can you submit a documentation patch? I
would, but I'm too lazy to contribute. That doesn't mean I'm not
thankful for your efforts, though!

p

Or if not a doc patch, how about a limerick?

Michael

Jul 18 '05 #26
Martin Ambuhl wrote:
Apologies to these various newsgroups, but I have no idea which one Xah
Lee actually reads or posts from. F'ups set.


Probably none, since they are all crowded with crazy linux zealots :-D.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Jul 18 '05 #27
>Comparisons can be chained, and is evaluated from left to right. For
example, x < y <= z is equivalent to (x < y) <= z.


The proposed 'correction' above is incorrect and should be ignored.
x,y,z = 2,3,1
x<y<=z 0 (x<y)<=z

1

Terry J. Reedy


Jul 18 '05 #28
Xah Lee wrote:
Very fucking stupid confusional writing.


I agree. Oh, were you talking about the Python documentation?

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Men live by forgetting -- women live on memories.
-- T.S. Eliot
Jul 18 '05 #29
Xah Lee wrote:
The word “object” has generic English meaning as well might have
very technical meaning in a language. In Python, it does not have very
pronounced technical meaning. For example, there's a chapter in Python
Library Ref titled “2. Built-In Objects”, and under it a section
“2.1 Built-in Functions”. Apparently, functions can't possibly be
meant as a “object” for comparisons.


Why not?
def foo(): .... pass
.... def bar(): .... pass
.... foo > bar

False
Jul 18 '05 #30
Martin Ambuhl wrote:
Xah Lee wrote:
The Python doc is relatively lousy, from content organization to
the tech writing quality.

I think i'll just post snippets of my comments as i find them.
(and feel like exposing)


The cross-posting idiot is back. Next he'll be posting his
readings from Richard Burton's _Arabian Nights_.

Xah, for you to think that Python is topical in the newsgroups to
which you posted, you must be sharing Karl Malbrain's cell at the
loony bin.

Apologies to these various newsgroups, but I have no idea which
one Xah Lee actually reads or posts from. F'ups set.


Unfortunately once the imbecile has done such a crosspost the only
cure it to immediately PLONK the thread.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Jul 18 '05 #31
there is a Python, pithy
mighty, lissome, and tabby
algorithms it puffs
tim-toady it engulfs
and sways universality
there is a camel, lanky
ugly, petty, ungainly
foolhardy comports
hacking it supports
and toadies eunuch's fancy

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Michael Spencer wrote:
Or if not a doc patch, how about a limerick?

Michael


Jul 18 '05 #32
umm... looks like it should've been:

Comparison can be chained, and is equivalent to a sequence of
comparisons with “and” in between. For example, “x<y<=z” is
effectively “(x<y) and (y<=z)”

Xah
xa*@xahlee.org
http://xahlee.org/

Terry Reedy wrote:
Comparisons can be chained, and is evaluated from left to right. For
example, x < y <= z is equivalent to (x < y) <= z.


The proposed 'correction' above is incorrect and should be ignored.
x,y,z = 2,3,1
x<y<=z 0 (x<y)<=z

1

Terry J. Reedy


Jul 18 '05 #33
In comp.lang.perl.misc Xah Lee <xa*@xahlee.org> wrote:
The Python doc is relatively lousy, from content organization to the
tech writing quality.
Which has precisely what to do with most of the groups to which you
cross-posted?
Problem: They all have the same priority (which is higher than that of
the Boolean operations). However, the ?they? pronoun from the context of previous sentence,
refers to ?the comparison operation?, not ?operator?. So, it
conjures the reader to think about some ?operation precedence?,
which in itself cannot be ruled out as nonsense depending on the
context. Very fucking stupid confusional writing.
The meaning is perfectly plain. By the way did you perhaps mean
'confusing' or 'confused'? I normally regard language flames
otiose but here, since you are trying to correct other people's
writing, fair game.
And, from pure writing aspect, the sentence ?...(which is ...)? is
some kind of a juvenile latch on. If the author intent to make that
Do you mean... 'if the author's intent is to make' or 'if the author
intends to'?
point, say it in its own sentence. e.g. The comparison operators have
higher precedence than boolean operators. It would be better to not
mention this at all. For practical considerations, very rare is the
case of mixing boolean and comparison operators, and if so, parenthesis
are likely used and is indeed a good practice. The proper place for
Should that not be 'parentheses are likely to be used'?

It is not rare at all. Besides, little asides like this are an ideal
way to reinforce thoughts about operator preference and more helpful
then memorising a table.
Problem: Comparisons can be chained arbitrarily; for example, x < y <=
z is equivalent to x < y and y <= z, except that y is evaluated only
once (but in both cases z is not evaluated at all when x < y is found
to be false). Drop the word ?arbitrarily?. It has no meaning here.
I suggest you look up the meaning of the word.
the whole sentence is one fucked up verbiage of pell-mell thinking and
writing. Here's one example of better: Comparisons can be chained, and is evaluated from left to right. For
example, x < y <= z is equivalent to (x < y) <= z.
You mean 'are evaluated'. Your rephrasing is also incomplete since
it doesn't note that z will not be evaulated if x < y is false.
With respect to documentation style, it is questionable that this
aspect needs to be mentioned at all. In practice, if programers need to
chain comparisons, they will readily do so. This is not out of ordinary
in imperative languages, and evaluation from left to right is also not
extraordinary to cost a mention.
Really? In a reference manual such matters should be fully specified
regardless of what happens in other languages.
Problem: <> and != are alternate spellings for the same operator. != is
the preferred spelling; <> is obsolescent In general, when something is obsolete or might go defunct in the
future, consider not even mentioning that construct. If necessary, add
it in a obscure place, and not adjacent to critical info. In many
places of Python documentation, this is breached.
Really? Actually it is very important to mention it in the correct
place in a reference manual. Do you understand the concept of
maintaining old code?
I'm in fact somewhat surprised by this poor quality in writing. The
Really? The writing in the manual seems simple, straight-forward and
clear to me (knowing no Python). Unlike your writing.
more egregious error is the hardware-oriented organization aka
technical drivel.
What on earth does this mean?
The Python doc, though relatively incompetent, but the author have
tried the best. This is in contrast to documentations in unix related
things (unix tools, perl, apache, and so on etc), where the writers
have absolutely no sense of clear writing, and in most cases don't give
a damn and delight in drivel thinking of it as literary.


I think that this is an excellent description of your own writing.

Axel

Jul 18 '05 #34
ax**@white-eagle.invalid.uk wrote:
The Python doc, though relatively incompetent, but the author have
Really, how could those morons even dream of creating a language,
and even writing docs to accompany it??
tried the best. This is in contrast to documentations in unix related
things (unix tools, perl, apache, and so on etc), where the writers
have absolutely no sense of clear writing, and in most cases don't give
a damn and delight in drivel thinking of it as literary.

Well, man-pages are at least coherent and precise.
It's not literature, it's technical documentation!

I think that this is an excellent description of your own writing.


:)

To be sure, he's a stupid troll, but I think you shouldn't insult
him for being bad at English. I bet you (or most Western people
anyway) have trouble getting fluent in an Asian language. Imagine
the lingua franca were Chinese, non English...
Jul 18 '05 #35
Better:

there is a Python, pithy
mighty, lissome, and tabby
algorithms it puffs
conundrums it snuffs
and cherished by those savvy
there is a camel, kooky
ugly, petty, ungainly
hacking it supports
TIMTOWTDI it sports
and transports DWIM-wit's fancy

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #36
Please stop cross-posting this stuff!

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 18 '05 #37
Xah Lee wrote:
Better:

there is a Python, pithy
mighty, lissome, and tabby
algorithms it puffs
conundrums it snuffs
and cherished by those savvy
there is a camel, kooky
ugly, petty, ungainly
hacking it supports
TIMTOWTDI it sports
and transports DWIM-wit's fancy

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html


These "limericks" just don't work, do they? I mean, the number of
syllables is just way off, and then the rhymes are just plain wrong. By
all means, don't stop trying, but don't quit your day job yet.
Jul 18 '05 #38
In article <_w41e.74579$SF.14703@lakeread08>,
Andras Malatinszky <no****@dev.null> wrote:
Xah Lee wrote:
Better:

there is a Python, pithy
mighty, lissome, and tabby
algorithms it puffs
conundrums it snuffs
and cherished by those savvy
there is a camel, kooky
ugly, petty, ungainly
hacking it supports
TIMTOWTDI it sports
and transports DWIM-wit's fancy

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html


These "limericks" just don't work, do they? I mean, the number of
syllables is just way off, and then the rhymes are just plain wrong. By
all means, don't stop trying, but don't quit your day job yet.


A poetry critic, Andras
In a huff at the tempo, alas!
The meter's too free,
So he's telling Xah Lee,
Nocturnal employment won't last!
Jul 18 '05 #39
Python doc “3.6.4 Mutable Sequence Types” at
http://python.org/doc/2.4/lib/typesseq-mutable.html

in which contains the documentation of the “sort” method ofa list.
Quote:

--------------
..sort([cmp[, key[, reverse]]])
sort the items of s in place
(7), (8), (9), (10)

....

(8)

The sort() method takes optional arguments for controlling the
comparisons.

cmp specifies a custom comparison function of two arguments (list
items) which should return a negative, zero or positive number
depending on whether the first argument is considered smaller than,
equal to, or larger than the second argument: "cmp=lambda x,y:
cmp(x.lower(), y.lower())"

key specifies a function of one argument that is used to extract a
comparison key from each list element: "cmp=str.lower"

reverse is a boolean value. If set to True, then the list elements
are sorted as if each comparison were reversed.

In general, the key and reverse conversion processes are much faster
than specifying an equivalent cmp function. This is because cmp is
called multiple times for each list element while key and reverse
touch each element only once.

Changed in version 2.3: Support for None as an equivalent to omitting
cmp was added.

Changed in version 2.4: Support for key and reverse was added.

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

As a piece of documentation, this is a relatively lousy one.

The question Python doc writers need to ask when evaluating this piece
of doc are these:

* can a experienced programer (expert at several languages) who is new
to Python, and also have read the official Python tutorial, can he,
read this doc about ”sort”, and know exactly how to use it with all
the options?

* can this piece of documentation be rewritten fairly easily, so that
the answer to the previous question is a resounding yes?

To me, the answers to the above questions are No and Yes. Here are some
issues with the doc:

* in the paragraph about “Key”, the illustration given is:
“cmp=str.lower”. Shouldn't it be “key=str.lower”?

* This doc lacks examples. One or two examples will help a lot,
especially to less experienced programers. (which comprises the
majority of readers) In particular, it should give a full example of
using the cmp predicate and one with key.

* This doc fails to address what happens when the predicate and the
short cut version conflicts. e.g.
myList.sort(lambda x,y: cmp(x[0], y[0]), lambda x: str(x[0]), False )

* on the whole, the way this doc is written does not give a clear
picture of what exactly is happening (think of mathematics) with sort,
nor the roles of the supplied options, nor how to use them.

Quick remedy: add a examples of using cmp predicate. And a example
using “key” predicate. Add example of Using one of them andwith
reverse. (the examples need not to come with much explanations. One
sentence annotation will do. )

Other than that, the way the doc is layed out with a terse table and
run-on footnotes (as appeared in other pages) is not inductive. For a
better improvement, there needs to be overhaul of the organization and
the attitude of the entire doc. The organization needs to be programing
based, as opposed to implementation or computer science based. (in this
regard, one can learn from the Perl folks). As to attitude, the writing
needs to be Python-as-is, as opposed to computer science framework.
I'll might have to give more details in the future, but I've made some
headway in this respect before; peruse:

http://xahlee.org/Periodic_dosage_di...ami_cukta.html

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html



Jul 18 '05 #40
On 28 Mar 2005 03:56:42 -0800, in comp.lang.c , "Xah Lee"
<xa*@xahlee.org> wrote:
Python doc 3.6.4 Mutable Sequence Types at


(whatever)

will you /please/ stop crossposting this python thread to comp.lang.c.

(FUP's set to exclude CLC, please respect this)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jul 18 '05 #41

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

Similar topics

3
by: Dave | last post by:
Hello all, When working with the STL, under what circumstances may I use a function object that modifies its internal state from one call to the next? I know this can be done with for_each<>()....
2
by: Samir | last post by:
I don't know if this is easy or not, I have no clue on how to start it. I have Seven different states. I will be having a form with 7 textboxes, and I will enter 7 different states each time in...
1
by: grandeandy | last post by:
question: in the code below, and in general, how would I input multiple functions into the code, and get multiple outputs? basically, I want to be able to add say 2 or 3 of the div's to the link so...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
2
by: sqweek | last post by:
Hiya. I've just been implementing a finite state machine a couple of times to compare the performance of different approaches. One of my attempts was to have a function for each state, which takes...
1
by: nik | last post by:
hello friends, I have a serious problem .i have a base class having 15 member function and there r 4 classes derived from this base class .the situation is like if certain condion is met then the...
20
by: asdf | last post by:
True or false ?
4
by: Danny Shevitz | last post by:
Simple question here: I have a multiline string representing the body of a function. I have control over the string, so I can use either of the following: str = ''' print state return True...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.