Common Python Mistakes
toptal.com
Common Python Mistakes
1–10 of 146 posts
Re: Common Python Mistakes
#2 return x,
I have never wanted to declare a tuple without surrounding it with (). Too bad it's not a syntax error in python 3.Also, as opposed to one of his examples, if you are using python 2.7, declare your exception blocks as:
except (FooException, BarException) as e:
It's forward compatible with python 3, it's easier to read and the syntax errors are clearer.Re: Common Python Mistakes
#3"when the default value for a function argument is an expression, the expression is evaluated only once"
I would explain the behavior he shows as due to the default value being mutable. I don't see an expression there, just an empty list used as a default.
Re: Common Python Mistakes
#4This actually happens when the function is defined, not when it's called the first time.
Re: Common Python Mistakes
#5 def foo2():
def foo(a=[]):
a.append('ba')
return a
return foo()
print foo2()
print foo2()
print foo2()
print foo2()
print foo2()
Gives ['ba']
['ba']
['ba']
['ba']
['ba']Re: Common Python Mistakes
#6Is the explanation for #1 correct? "when the default value for a function argument is an expression, the expression is evaluated only once" I would explain the behavior he shows as due to the default value being mutable . I don't see an expression there, just an empty list used as a default.
def f(now=datetime.datetime.now()):
...
now would be the time when the module was loaded, not when f is called the first time, or when f is called after that, despite datetime objects being immutable.Re: Common Python Mistakes
#7Is the explanation for #1 correct? "when the default value for a function argument is an expression, the expression is evaluated only once" I would explain the behavior he shows as due to the default value being mutable . I don't see an expression there, just an empty list used as a default.
The default value is an expression which is executed when the module is instantiated, even if the expression results in an empty list. If I had: def f(now=datetime.datetime.now()): ... now would be the time when the module was loaded, not when f is called the first time, or when f is called after that, despite datetime objects being immutable.
Re: Common Python Mistakes
#8I have an issue with that statement. No languages are inherently "compiled" or "interpreted", that's a property of the implementation.
If we are talking about CPython here, Python code is compiled to bytecode which is then interpreted. Not unlike Java - with the difference that the main implementation has a JIT and afaik, Python's does not.
But that's CPython. What about PyPy? It has a JIT.
Re: Common Python Mistakes
#9Is the explanation for #1 correct? "when the default value for a function argument is an expression, the expression is evaluated only once" I would explain the behavior he shows as due to the default value being mutable . I don't see an expression there, just an empty list used as a default.
The default value is an expression which is executed when the module is instantiated, even if the expression results in an empty list. If I had: def f(now=datetime.datetime.now()): ... now would be the time when the module was loaded, not when f is called the first time, or when f is called after that, despite datetime objects being immutable.
import datetime, time
def foo2():
def foo(a=datetime.datetime.now()):
time.sleep(1)
return a
return foo()
for n in range(10): print foo2()
2014-05-08 11:23:01.642871
2014-05-08 11:23:02.644276
2014-05-08 11:23:03.644579
2014-05-08 11:23:04.645146
2014-05-08 11:23:05.646328
2014-05-08 11:23:06.647572
2014-05-08 11:23:07.647904
2014-05-08 11:23:08.648213
2014-05-08 11:23:09.648973
2014-05-08 11:23:10.649742
So, not quite module load time, but evaluation time.Re: Common Python Mistakes
#10My pet peeve with python is the classic: return x, I have never wanted to declare a tuple without surrounding it with (). Too bad it's not a syntax error in python 3. Also, as opposed to one of his examples, if you are using python 2.7, declare your exception blocks as: except (FooException, BarException) as e: It's forward compatible with python 3, it's easier to read and the syntax errors are clearer.
No? I do that occasionally, e.g.:
x, y = 5, 6