3

I'm diving deeper into Python after experience with several other languages. In order to be 'pythonic' and from a best practice perspective, should methods of a class always return (even if None)? In some languages it's required (or preferred) to always have an explicit return statement.

For example ,a method that alters state but doesn't exactly return a value:

def change_player():
    if self._player == 'X':
        self._player = 'Y'
    elif self._player == 'Y'
        self._player = 'X'
    else: 
        pass

Should there be an explicit return statement here at the close of the function?

Fred G
  • 31
  • 1
  • 2
  • 1
    One thing: you never need `else: pass` because if you leave it out, the same thing will happen. – zondo Mar 13 '16 at 17:16
  • Possible duplicate of [Python -- return, return None, and no return at all](http://stackoverflow.com/questions/15300550/python-return-return-none-and-no-return-at-all) – Reti43 Mar 13 '16 at 17:21
  • Also note that [PEP0008](https://www.python.org/dev/peps/pep-0008/#public-and-internal-interfaces) says *"Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None , and an explicit return statement should be present at the end of the function (if reachable)."* – Reti43 Mar 13 '16 at 17:23

4 Answers4

3

Exceptions, not return codes, should generally be used to indicate errors. Return values are acceptable if you are expecting some kind of output from a function or method. Your code should probably look like this:

def change_player(self):
    if self._player == 'X':
        self._player = 'Y'
    elif self._player == 'Y':
        self._player = 'X'
    else: 
        raise ValueError 

Here is some discussion on how using exceptions in Python is a cleaner way of doing things: http://jeffknupp.com/blog/2013/02/06/write-cleaner-python-use-exceptions/

cs01
  • 5,287
  • 1
  • 29
  • 28
1

A method should either set an attribute or return a value, not both at once. So your method is totally pythonic.

def change_player():
    if self._player == 'X':
        self._player = 'Y'
    elif self._player == 'Y'
        self._player = 'X'
Daniel
  • 42,087
  • 4
  • 55
  • 81
1

A return statement is certainly not required.

As to whether it is good style to include one or not, I would say no - but that is basically a matter of opinion.

1

When a function modifies something, it has a side effect. Python is a flexible language and allows you to do things your way to a certain limit. However, a function should be dedicated to do something very brief, for instance:

  • Receive an argument, do something with it, and return the new value
  • Receive an argument, use the value to modify something, and raise an Exception if the process was unsuccessful.
  • Receive nothing, modify something, and raise an Exception if an error arise or at least return a flag (i.e. an integer) to notify the state of the function.

It is a good idea to return something or raise an Exception to always know if the function works to avoid quiet bugs/side effects. In strict functional programming, a function shouldn't produce any kind of side effects at all.

Pandemonium
  • 7,724
  • 3
  • 32
  • 51