Returns a variable vs returns a function call

What is the recommended Pythonic method for returning values ​​from a method and why? Is there any documentation about this in the PEP8 style guide, I could not find it.

Method 1

def method():
    a = meth2()
    return a

Method 2

def method():
    return meth2()
+4
source share
4 answers

PEP8 does not indicate whether to return a variable depending on the function.

However, he says you should be consistent:

return. return , . - , , , return None, return ( ).

# Yes
def myfunction(a: int, b: int) -> int:
    if a % 2 == 0:
        return int(a ** b)
    else:
        return 0

# No
def my_bad_function(a: int, b: int) -> int:
    if a % 2 == 0:
        return int(a ** b)
    # Implicitly returns None when the above if statement evaluates False

( PEP8), .
, . None.
, , , , , int, int.bit_length()

, :

for n in range(1, 10):
    nlen = my_bad_function(n * 5, n).bit_length()
+3

1, .

1 , , , return a , a. (, ++ Boost 2 , - CPU!)

python , , .

+2

2 , , , . , : P

1 - ,

+1

1 bcos, ( ?)

If I am debugging code that is mehtod 2, then I use the evalate Expression parameter to check, returning a return statement.

0
source

Source: https://habr.com/ru/post/1666704/


All Articles