E
Egor Bolonev
why functions created with lambda forms cannot contain statements?
how to get unnamed function with statements?
how to get unnamed function with statements?
Egor said:why functions created with lambda forms cannot contain statements?
how to get unnamed function with statements?
Op 2005-01-13 said:Because if it takes more than a single line it deserves a name.
Antoon said:So if I have a call with an expression that takes more than
one line, I should assign the expression to a variable and
use the variable in the call?
But wait if I do that, people will tell me how bad that it
is, because it will keep a reference to the value which
will prevent the garbage collector from harvesting this
memory.
But it's discouraged in general.Besides python allows more than one statement on the same line.
hanz said:Nobody will tell you that it's bad. Python was never about super
performance, but about readability. Besides, using such temporaries
won't consume much memory (relatively).
Paul said:That completely depends on the objects in question. Compare
temp = all_posters[:]
temp.sort()
top_five_posters = temp[-5:]
to:
top_five_posters = all_posters.sorted()[-5:]
which became possible only when .sorted() was added to Python 2.4.
Steven Bethard said:Note that sorted is a builtin function, not a method of a list
object.
Paul said:Oh, same difference. I thought it was a method because I'm not using
2.4 yet. The result is the same
other than that having it as a function instead of a method is another
inconsistency to remember
Fredrik Lundh said:nope. sorted works on any kind of sequence, including forward-only
iterators. sorted(open(filename)) works just fine, for example.
Egor Bolonev said:why functions created with lambda forms cannot contain statements?
Paul Rubin said:Oh, same difference. I thought it was a method because I'm not using
2.4 yet. The result is the same, other than that having it as a
function instead of a method is another inconsistency to remember.
Op 2005-01-13 said:Yes, that's sometimes a good practice and can clarify
the call.
Nobody will tell you that it's bad.
"A foolish consistency is the hobgoblin of little minds". Rules are madeSorry, someone already did. If I recall correctly it
was Alex Martelli.
Op 2005-01-14 said:Of course, unless that reference is in the global scope of the __main__
module its lifetime will be transient anyway. If the reference is stored
in a function's local variable then unless its value is returned from
the function it will become available for garbage collection when the
function returns.
"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken.
Besides which, if you don't understand the language
environment, rules alone will do you very little good. Try to focus a
little more on principles and a little less on minutiae.
Fair enough, but don;t go advising newbies to do this.Like only use immutables as dictionary keys.
Principle: "Ten angels can dance on the head of a pin".And what are the difference between those two?
Sometimes I get the impression that everything is a principle until
one personnaly finds the need to break it. After that it is a rule.
Op 2005-01-17 said:Antoon Pardon wrote:
[...]Fair enough, but don;t go advising newbies to do this.Like only use immutables as dictionary keys.
There you go with the minutiae again. How about:Antoon said:Op 2005-01-17 said:Antoon Pardon wrote:
[...]
"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken.
Like only use immutables as dictionary keys.
Fair enough, but don;t go advising newbies to do this.
How about something like this.
Because of the extra precautions one has to take when
using mutables as hash keys, we advise newbies
to stick with immutable keys until they have gathered
enough knowledge and experience to adequatly weight
the pro and cons of a mutable key solution against
an immutable key solution.
Op 2005-01-17 said:There you go with the minutiae again. How about:Antoon said:Op 2005-01-17 said:Antoon Pardon wrote:
[...]
"A foolish consistency is the hobgoblin of little minds". Rules are made
to be broken.
Like only use immutables as dictionary keys.
Fair enough, but don;t go advising newbies to do this.
How about something like this.
Because of the extra precautions one has to take when
using mutables as hash keys, we advise newbies
to stick with immutable keys until they have gathered
enough knowledge and experience to adequatly weight
the pro and cons of a mutable key solution against
an immutable key solution.
"Don't use mutables as hash keys"?
Antoon Pardon said:Op 2005-01-17, Steve Holden schreef <[email protected]>:
That sounds too dogmatic to my ears. I also find it
too selective. The problem with mutables as dictionary
keys is not specific to dictionaries. Everywhere you
have mutables in a container, it is possible that
mutating the object in the container will cause
problem.
Heck even using mutables as arguments can
cause trouble. Why else the specific advice against
def foo(p = [])
type of arguments. So should we adopt the principles:
Don't use mutables in containers
Nonsense.
Don't use mutables as default values for parameters
Don't use mutables as arguments.
Nonsense.
Don't assign one mutable to an other.
I don't see a big difference between these principles
and the hash key principle,
so in the end may be we
should just stick with the more general principle:
Don't use mutables!
and be done with it.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.