Severely, since what you're doing is mutating the actual state-data of the query object.
for each in enumerate(items):
if each.match(self):
# This fails because enumerate yields an (index, value) tuple.
# Match isn't a function defined on tuples.So methods that mutate state, and don't return None are un-Pythonic?
What about this example:
a = [1, 2, 3]
b = a.pop()
pop() mutates the state of a and returns a value that is not None. Is the Python code language itself un-Pythonic?I'm curious, as someone who uses this pattern quite a bit, how would you improve on this?
In [1]: def foo(): pass
In [2]: foo() is None
Out[2]: True> if a method does not use the object's state (no `self` usage) make it a `class-` or `staticmethod`.
Uh, that's a little too "cookbook" for my tastes. The decision of whether to make something a class / static method or not should be decided based on what the function is doing, rather than some pointless dogma. I'm assuming he means this method:
> def alarm(self): > with open(BUZZER_MP3_FILE) as f: > play_sound(f.read())
It's a design decision, and one that is particularly trivial at that. Let's say the effects an alarm ultimately change, resulting in an internal state read / write on the object. Now you have to go back and update the class despite the fact that it at a higher philosophical level one could argue that an alarm should only be triggered on an individual boiler.
Point being: it's nit-pickery for nit-pickery's sake. I don't personally use `return None`, but it's certainly more explicit and is fine to use if your judgment has found it to make sense.
For example, when looking through someone elses code, if it does not have deep levels of nesting I prefer longer methods you can read like a script rather than having to jump around the place to see what is each method.
Likewise with the 80 character guideline in PEP. It can take a lot longer to find the closing brace if it is hidden in columns that look like a newspaper article.
Definitely. The PEP8 was actually revised 6 months ago[0]. Nothing crazy, but a bit more pragmatic.
>For code maintained exclusively or primarily by a team that can reach agreement on this issue, it is okay to increase the nominal line length from 80 to 100 characters (effectively increasing the maximum length to 99 characters)
0: text: http://legacy.python.org/dev/peps/pep-0008/#maximum-line-len...
Compare:
def makeMove(self, target):
enemy_piece = target.piece
if enemy_piece:
self.takeEnemyPiece(enemy_piece)
target.piece = self
self.position = target
And def changeCoords(self, foo):
if foo.standing_on_it:
self.nowItsMine(foo.standing_on_it)
foo.standing_on_it = self
self.coords = foo
In the former case, you mostly don't need to consult the source of functions called. In the latter, you're never sure.What I really disliked is storing intermediate results of a multi-stage computation in instance variables. It is so easy to return multiple values from Python functions. Also, thinking what you need and need not pass and return between interconnected functions helps structure them much better, into smaller, clearer pieces (as #1 correctly advises).
simply `return` would be enough
At this point I think calling something idiomatic or Pythonic is a thought terminating statement. I need to see refutation.
In terms of refutation, some things are just conventions, and breaking them has to be done with the understanding that the benefits outweigh the costs. If python were a clean slate, totally new language, you might have a discussion about the merits of mutating chaining, but as it is, the convention is not to do that, and the benefits don't buy enough to violate everyone's expectations.
* if a method does not use the object's state (no `self` usage) make it a `class-` or `staticmethod`.
* Some magic methods are presented. There's more to them[0].
* one should not write `class MyClass:` but `class MyClass(object):` (new style class[1])
* the last one (`return None`) make me very dubious
* Cascading methods: that's a big no. The idiom is that if a method may change the state of the object then it should return None (eg `set.add`)
0: well-written and comprehensive guide: http://www.rafekettler.com/magicmethods.html
1: http://www.python.org/doc/newstyle/