language-design

Why does Python assignment not return a value?

Why does Python assignment not return a value? Question: Why is Python assignment a statement rather than an expression? If it was an expression which returns the value of the right hand side in the assignment, it would have allowed for much less verbose code in some cases. Are there any issues I can’t see? …

Total answers: 5

Read/Write Python Closures

Read/Write Python Closures Question: Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another …

Total answers: 8

Why doesn't Python have a sign function?

Why doesn't Python have a sign function? Question: I can’t understand why Python doesn’t have a sign function. It has an abs builtin (which I consider sign‘s sister), but no sign. In python 2.6 there is even a copysign function (in math), but no sign. Why bother to write a copysign(x,y) when you could just …

Total answers: 12

Why are default arguments evaluated at definition time?

Why are default arguments evaluated at definition time? Question: I had a very difficult time with understanding the root cause of a problem in an algorithm. Then, by simplifying the functions step by step I found out that evaluation of default arguments in Python doesn’t behave as I expected. The code is as follows: class …

Total answers: 9

Can't set attributes on instance of "object" class

Can't set attributes on instance of "object" class Question: So, I was playing around with Python while answering this question, and I discovered that this is not valid: o = object() o.attr = ‘hello’ due to an AttributeError: ‘object’ object has no attribute ‘attr’. However, with any class inherited from object, it is valid: class …

Total answers: 7

Why doesn't a python dict.update() return the object?

Why doesn't a python dict.update() return the object? Question: I have this code: award_dict = { "url": "http://facebook.com", "imageurl": "http://farm4.static.flickr.com/3431/3939267074_feb9eb19b1_o.png", "count": 1, } def award(name, count, points, desc_string, my_size, parent): if my_size > count: a = { "name": name, "description": desc_string % count, "points": points, "parent_award": parent, } a.update(award_dict) return self.add_award(a, siteAlias, alias).award But the …

Total answers: 11

"Least Astonishment" and the Mutable Default Argument

"Least Astonishment" and the Mutable Default Argument Question: Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function called with no parameter to always return a list with only one element: [5]. The result is instead very …

Total answers: 34

Elegant ways to return multiple values from a function

Elegant ways to return multiple values from a function Question: It seems like in most mainstream programming languages, returning multiple values from a function is an extremely awkward thing. The typical solutions are to make either a struct or a plain old data class and return that, or to pass at least some of the …

Total answers: 14