Live data from Hacker News

Common Python Mistakes

toptal.com

1–10 of 146 posts

Re: Common Python Mistakes

#2
My 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.

Re: Common Python Mistakes

#3
Is 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.

Re: Common Python Mistakes

#4
"Thus, the bar argument is initialized to its default (i.e., an empty list) only the first time that foo() is called, but then subsequent calls to foo() (i.e., without a bar argument specified) will continue to use the same list to which bar was originally initialized."

This actually happens when the function is defined, not when it's called the first time.

Re: Common Python Mistakes

#5
Also, for the first example, it is a scope issue:

  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

#6

Is 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

#7
post #6

Is 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.

Isn't that just now being bound to a different datetime object each time f is defined?

Re: Common Python Mistakes

#8
> "Python is an interpreted, object-oriented, high-level programming language with dynamic semantics."

I 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

#9
post #6

Is 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

#10
post #2

My 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.

I have never wanted to declare a tuple without surrounding it with ().

No? I do that occasionally, e.g.:

  x, y = 5, 6
Post reply on HN