On Sat, 24 Dec 2011 19:41:55 +0100, Thomas Rachel wrote:
The only times you need the brackets around a tuple is to control the
precedence of operations, or for an empty tuple.
IBTD:
a=((a, b) for a, b, c in some_iter)
b=[(1, c) for whatever]
Without the round brackets, it is a syntax
On Sat, 24 Dec 2011 09:50:04 +1100, Chris Angelico wrote:
On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
Yes. But having to manage it *by hand* is still unclean:
Well, my point was that Python's current behaviour _is_ that.
Minus the managing
On Fri, 23 Dec 2011 19:24:44 -0500, Devin Jeanpierre wrote:
To fake early binding when the language provides late binding, you
still use a sentinel value, but the initialization code creating the
default value is outside the body of the function, usually in a global
variable:
_DEFAULT_Y
On Fri, 23 Dec 2011 17:03:11 +, Neil Cerutti wrote:
The disadvantage of late binding is that since the expression is live,
it needs to be calculated each time, even if it turns out to be the
same result. But there's no guarantee that it will return the same
result each time:
That's its
On 12/22/2011 10:20 AM, Dennis Lee Bieber wrote:
which is to define the names a, b, and c, and connects the three
names to the single object (integer 7 or new empty list).
note that this connects and disconnecting business is more commonly
referred to in python parlance as binding a name to
On Dec 24, 2:27 am, Mel Wilson mwil...@the-wire.com wrote:
In a tool that's meant for other people to use to accomplish work of their
own, breaking workflow is a cardinal sin.
In a research language that's meant always to be up-to-date with the concept
of the week, not so much.
What on earth
On Dec 24, 2:15 am, Roy Smith r...@panix.com wrote:
I know this is not quite the same thing, but it's interesting to look at
what django (and mongoengine) do in their model definitions, prompted by
your time.time() example. You can do declare a model field something
like:
class
On Dec 24, 6:25 pm, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:
It's
much harder to figure out what's going wrong with an early-bound
mutable.
Only for those who don't understand, or aren't thinking about, Python's
object model. The behaviour of early-bound mutables is
Am 21.12.2011 23:25 schrieb Eric:
Is it true that if I want to create an array or arbitrary size such
as:
for a in range(n):
x.append(some function...)
I must do this instead?
x=[]
for a in range(n):
x.append(some function...)
Of course - your x must exist before
Am 22.12.2011 00:48 schrieb Steven D'Aprano:
On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote:
For the amount of typing, it's easier to just do a straight line
tuple unpack
a,b,c = ([],[],[])
Note that tuples are created by the comma, not the round brackets (or
Am 22.12.2011 00:20 schrieb Dennis Lee Bieber:
The key one is that lists ([] defines a list, not an array) are
mutable. Your 7 is not mutable.
Strictly spoken, that's only a side show where the effect is visible.
The real key concept is that [] creates *one* object which is then
If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd
see an increase in the number of questions like the OP's.
What makes you so sure? Both models do make sense and are equally
valid, it's just that only one of them is true. Is it just because
people already used to Python
On Dec 25, 9:25 am, Devin Jeanpierre jeanpierr...@gmail.com wrote:
If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd
see an increase in the number of questions like the OP's.
What makes you so sure? Both models do make sense and are equally
valid, it's just that only
Because I believe that the source of confusion has far more to do with
mutable/immutable objects than with early/late binding. Masking or
'correcting' an aspect of Python's behaviour because novices make the
wrong assumption about it just pushes the problem elsewhere and
potentially makes the
On Dec 25, 5:32 am, Devin Jeanpierre jeanpierr...@gmail.com wrote:
alex23 wrote:
Because I believe that the source of confusion has far more to do with
mutable/immutable objects than with early/late binding. Masking or
'correcting' an aspect of Python's behaviour because novices make the
On 12/24/2011 07:25 PM, Steven D'Aprano wrote:
I'd use a function attribute.
def func(x, y=None):
if y is None:
y = func.default_y
...
func.default_y = []
That's awkward only if you believe function attributes are awkward.
I do. All you've done is move the default from *before*
On Dec 23, 7:10 am, alex23 wuwe...@gmail.com wrote:
On Dec 22, 6:51 pm, Rolf Camps r...@roce.be wrote:
I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default' always points to the same
Ian Kelly wrote:
On Thu, Dec 22, 2011 at 7:10 PM, alex23 wuwe...@gmail.com wrote:
On Dec 22, 6:51 pm, Rolf Camps r...@roce.be wrote:
I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default'
On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote:
Likewise function arguments that default to mutable entities is a known
gotcha of python which is best treated as a bug in python.
Nonsense. It is a feature, not a bug.
Some people might argue that it is a mistake, a minor feature which
rusi wrote:
On Dec 23, 7:10 am, alex23 wuwe...@gmail.com wrote:
On Dec 22, 6:51 pm, Rolf Camps r...@roce.be wrote:
I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default' always points to the
On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman et...@stoneleaf.us wrote:
That is the most ridiculous thing I have heard in a while. Mutable default
arguments are *not* a bug in Python.
Reminds me of a bug report a couple years back claiming multiple inheritence
was a bug and asking it to be
On Dec 23, 2:59 pm, Chris Angelico ros...@gmail.com wrote:
On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman et...@stoneleaf.us wrote:
That is the most ridiculous thing I have heard in a while. Mutable default
arguments are *not* a bug in Python.
Reminds me of a bug report a couple years back
On Dec 23, 2:39 pm, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:
On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote:
Likewise function arguments that default to mutable entities is a known
gotcha of python which is best treated as a bug in python.
Nonsense. It is a feature, not
On 12/23/11 10:22 AM, rusi wrote:
On Dec 23, 2:39 pm, Steven D'Apranosteve
+comp.lang.pyt...@pearwood.info wrote:
On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote:
Likewise function arguments that default to mutable entities is a known
gotcha of python which is best treated as a bug in python.
On 2011-12-23, Neil Cerutti ne...@norwich.edu wrote:
Is the misfeature that Python doesn't evaluate the default
argument expression every time you call the function? What
would be the harm if it did?
...you know, assuming it wouldn't break existing code. ;)
--
Neil Cerutti
--
On 2011-12-23, Chris Angelico ros...@gmail.com wrote:
On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman et...@stoneleaf.us wrote:
That is the most ridiculous thing I have heard in a while.
?Mutable default arguments are *not* a bug in Python.
Reminds me of a bug report a couple years back
On Dec 23, 6:10 pm, Robert Kern robert.k...@gmail.com wrote:
On 12/23/11 10:22 AM, rusi wrote:
On Dec 23, 2:39 pm, Steven D'Apranosteve
+comp.lang.pyt...@pearwood.info wrote:
On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote:
Likewise function arguments that default to mutable
On 12/23/11 1:23 PM, rusi wrote:
On Dec 23, 6:10 pm, Robert Kernrobert.k...@gmail.com wrote:
On 12/23/11 10:22 AM, rusi wrote:
On Dec 23, 2:39 pm, Steven D'Apranosteve
+comp.lang.pyt...@pearwood.infowrote:
Some people might argue that it is a mistake, a minor feature which
allegedly
On Dec 23, 6:53 pm, Robert Kern robert.k...@gmail.com wrote:
On 12/23/11 1:23 PM, rusi wrote:
On Dec 23, 6:10 pm, Robert Kernrobert.k...@gmail.com wrote:
On 12/23/11 10:22 AM, rusi wrote:
On Dec 23, 2:39 pm, Steven D'Apranosteve
+comp.lang.pyt...@pearwood.info wrote:
Some
On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote:
On Dec 23, 6:53 pm, Robert Kern robert.k...@gmail.com wrote:
On 12/23/11 1:23 PM, rusi wrote:
[...]
Of course it should be fixed. The repeated recurrence of it as a
standard gotcha as well as the python ideas list testifies to that.
So you
On Fri, 23 Dec 2011 13:13:38 +, Neil Cerutti wrote:
On 2011-12-23, Neil Cerutti ne...@norwich.edu wrote:
Is the misfeature that Python doesn't evaluate the default argument
expression every time you call the function? What would be the harm if
it did?
...you know, assuming it wouldn't
On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
To fake early binding when the language provides late binding, you still
use a sentinel value, but the initialization code creating the default
value is outside the body of the function, usually in a
On Dec 23, 8:33 pm, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:
On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote:
On Dec 23, 6:53 pm, Robert Kern robert.k...@gmail.com wrote:
On 12/23/11 1:23 PM, rusi wrote:
[...]
Of course it should be fixed. The repeated recurrence of it
In article 4ef4a30d$0$29973$c3e8da3$54964...@news.astraweb.com,
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
The disadvantage of late binding is that since the expression is live, it
needs to be calculated each time, even if it turns out to be the same
result. But there's no
Steven D'Aprano wrote:
On Fri, 23 Dec 2011 13:13:38 +, Neil Cerutti wrote:
On 2011-12-23, Neil Cerutti ne...@norwich.edu wrote:
...you know, assuming it wouldn't break existing code. ;)
It will. Python's default argument strategy has been in use for 20 years.
Some code will rely on it.
On 2011-12-23, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
On Fri, 23 Dec 2011 13:13:38 +, Neil Cerutti wrote:
On 2011-12-23, Neil Cerutti ne...@norwich.edu wrote:
Is the misfeature that Python doesn't evaluate the default
argument expression every time you call the
On 12/23/2011 03:31 AM, rusi wrote:
In Fortran, if the comma in the loop
DO 10 I = 1,10
is misspelt as '.' it becomes the assignment
DO10I = 1.0
Do you consider it a bug or a feature?
Does Fortran consider it a bug or feature?
Non sequitor. Nothing at all to do with the issue at hand.
On Sat, 24 Dec 2011 02:55:41 +1100, Chris Angelico wrote:
On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
To fake early binding when the language provides late binding, you
still use a sentinel value, but the initialization code creating the
On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
Yes. But having to manage it *by hand* is still unclean:
Well, my point was that Python's current behaviour _is_ that.
* you still have to assign the default value to the function assignment
outside
To fake early binding when the language provides late binding, you still
use a sentinel value, but the initialization code creating the default
value is outside the body of the function, usually in a global variable:
_DEFAULT_Y = [] # Private constant, don't touch.
def func(x,
alex23 schreef op wo 21-12-2011 om 16:50 [-0800]:
On Dec 22, 8:25 am, Eric einazaki...@yahoo.com wrote:
This surprises me, can someone tell me why it shouldn't? I figure if
I want to create and initialize three scalars the just do a=b=c=7,
for example, so why not extend it to arrays.
Rolf Camps wrote:
alex23 schreef op wo 21-12-2011 om 16:50 [-0800]:
I'd say that _is_ the most pythonic way, it's very obvious in its
intent (or would be with appropriate names). If it bothers you that
much:
def listgen(count, default=[]):
for _ in xrange(count):
yield
On Dec 22, 6:51 pm, Rolf Camps r...@roce.be wrote:
I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default' always points to the same list.
I appreciate the concern, but adding a default
On Thu, Dec 22, 2011 at 7:10 PM, alex23 wuwe...@gmail.com wrote:
On Dec 22, 6:51 pm, Rolf Camps r...@roce.be wrote:
I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default' always points to the
On Dec 21, 6:50 pm, alex23 wuwe...@gmail.com wrote:
On Dec 22, 8:25 am, Eric einazaki...@yahoo.com wrote:
This surprises me, can someone tell me why it shouldn't? I figure if
I want to create and initialize three scalars the just do a=b=c=7,
for example, so why not extend it to arrays.
On Dec 23, 12:59 pm, Ian Kelly ian.g.ke...@gmail.com wrote:
It's only irrelevant in the immediate context of the code you posted.
But when Joe Novice sees your code and likes it and duplicates it a
million times
I'm sorry, but I'm not going to modify my coding style for the sake of
bad
On Fri, Dec 23, 2011 at 2:40 PM, alex23 wuwe...@gmail.com wrote:
I'm sorry, but I'm not going to modify my coding style for the sake of
bad programmers.
And there, folks, you have one of the eternal dilemmas. The correct
decision depends on myriad factors; if you're writing code to go into
the
On Dec 21, 5:44 pm, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:
Yes, you should create your lists before trying to append to them.
But you aren't forced to use a for-loop. You can use a list comprehension:
x = [some_function(a) for a in range(n)]
Notice that here you don't
On Thu, Dec 22, 2011 at 8:40 PM, alex23 wuwe...@gmail.com wrote:
On Dec 23, 12:59 pm, Ian Kelly ian.g.ke...@gmail.com wrote:
It's only irrelevant in the immediate context of the code you posted.
But when Joe Novice sees your code and likes it and duplicates it a
million times
I'm sorry, but
On Dec 23, 3:22 pm, Ian Kelly ian.g.ke...@gmail.com wrote:
Nobody is asking you to modify your coding style. The request is that
you not throw it up as an example without mentioning the important
caveats.
No, 100% no. It's not my responsibility to mention every potentially
relevant gotcha
On 21 December 2011 22:25, Eric einazaki...@yahoo.com wrote:
Is it true that if I want to create an array or arbitrary size such
as:
for a in range(n):
x.append(some function...)
I must do this instead?
x=[]
for a in range(n):
x.append(some function...)
Now to my actual
On Wed, Dec 21, 2011 at 2:25 PM, Eric einazaki...@yahoo.com wrote:
Is it true that if I want to create an array or arbitrary size such
as:
for a in range(n):
x.append(some function...)
I must do this instead?
x=[]
for a in range(n):
x.append(some function...)
You can
On Wed, 21 Dec 2011 14:25:17 -0800, Eric wrote:
Is it true that if I want to create an array or arbitrary size such as:
for a in range(n):
x.append(some function...)
x is not defined, so you will get a NameError unless by some lucky fluke
something else has created x AND it happens
On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote:
For the amount of typing, it's easier to just do a straight line
tuple unpack
a,b,c = ([],[],[])
Note that tuples are created by the comma, not the round brackets (or
parentheses for any Americans reading). So the round
On Dec 22, 8:25 am, Eric einazaki...@yahoo.com wrote:
This surprises me, can someone tell me why it shouldn't? I figure if
I want to create and initialize three scalars the just do a=b=c=7,
for example, so why not extend it to arrays.
The thing to remember is that everything is an object, and
55 matches
Mail list logo