Live data from Hacker News

A few things to remember while coding in Python

satyajit.ranjeev.in

141–146 of 146 posts

Re: A few things to remember while coding in Python

#141

Earlier quoted context omitted.

Ah. These are the little assumptions that keep blowing off my feet. Thanks.

It is these cases that brought the ternary operator to Python: def f(x=None): x if x is not None else []

I'm a little confused about this as an assignment to a variable. Is that because of the 'return' omission referred to below? If all function f did was return x, I could see how this works with a 'return' prepended.

But if this expression is a line in a larger function, and is intended to reset the value of argument x if no other value is passed in for it, does this really act as an assigment to x? Because I sort of read this expression as evaluating to some value -- the passed value for x, or a [] -- but does this assign that value to the argument x? Or must it be x = [expression]

Re: A few things to remember while coding in Python

#142

Earlier quoted context omitted.

It is these cases that brought the ternary operator to Python: def f(x=None): x if x is not None else []

I'm a little confused about this as an assignment to a variable. Is that because of the 'return' omission referred to below? If all function f did was return x, I could see how this works with a 'return' prepended. But if this expression is a line in a larger function, and is intended to reset the value of argument x if no other value is passed in for it, does this really act as an assigment to x? Because I sort of r…

  def f(x=None):
        x = x if x is not None else []
        return x
Now it will assign that value back to x. Otherwise it would just evaluate the expression.

Re: A few things to remember while coding in Python

#143

Earlier quoted context omitted.

I'm a little confused about this as an assignment to a variable. Is that because of the 'return' omission referred to below? If all function f did was return x, I could see how this works with a 'return' prepended. But if this expression is a line in a larger function, and is intended to reset the value of argument x if no other value is passed in for it, does this really act as an assigment to x? Because I sort of r…

def f(x=None): x = x if x is not None else [] return x Now it will assign that value back to x. Otherwise it would just evaluate the expression.

Thanks for the additional clarity.

Re: A few things to remember while coding in Python

#144
post #117

Earlier quoted context omitted.

I'm a Python newbie, and that code was immediately clear and obvious to me when I read it. I won't say it would be clear anyone that reads it, because we live in a world where people who claim to be programmers can't do fizz buzz.

I'm a Python veteran, and, if you do it like this, I'll shoot you. Add a @memoize decorator and do it there, you need to always be as obvious as possible. Compare: @memoize def fibonacci(n): pass def fibonacci(n, memory=[]): pass You don't even need documentation for the first example.

Starting from Python 3.2 there is a builtin decorator: http://docs.python.org/dev/library/functools.html#functools....

Re: A few things to remember while coding in Python

#145

Earlier quoted context omitted.

I'm a Python veteran, and, if you do it like this, I'll shoot you. Add a @memoize decorator and do it there, you need to always be as obvious as possible. Compare: @memoize def fibonacci(n): pass def fibonacci(n, memory=[]): pass You don't even need documentation for the first example.

Starting from Python 3.2 there is a builtin decorator: http://docs.python.org/dev/library/functools.html#functools....

That's fantastic, thanks for the link.

Re: A few things to remember while coding in Python

#146
post #125
post #110

Earlier quoted context omitted.

Good coverage. I use it quite often for cache dictionary, it's much simpler API and overall code, than creatng a new class for it. Demo snippet from effbot's site: def calculate(a, b, c, memo={}): try: value = memo[a, b, c] # return already calculated value except KeyError: value = heavy_calculation(a, b, c) memo[a, b, c] = value # update the memo dictionary return value

This seems a bit leaky. You're exposing the caching mechanism in the method signature(yeah, ok, in practice it's unlikely to be a problem).

The other option is to create your own cache object and pass that in (and around, if it's a recursive function). Of course, in Python pretty much every cache object follows the dictionary interface anyway, so it doesn't really matter. One of the benefits of duck typing :)
Post reply on HN