Both functions do the same thing.
def function1(self):
a = self.get_a()
b = self.get_b()
c = self.get_c()
r = None
if a:
r = a
if b:
r = b
if c:
r = c
else:
print("c not set.")
else:
print("b not set.")
else:
print("a not set.")
return r
def function2(self):
a = self.get_a()
b = self.get_b()
c = self.get_c()
r = None
if not a:
print("a not set.")
return r
r = a
if not b:
print("b not set.")
return r
r = b
if not c:
print("c not set.")
r = c
return r
function1() creates very long lines the more if's are nested which conflicts with PEP8's line-length limit of 78.
function2() might be harder to read/understand and has more return statements. Line length is no problem here.
Which one is more pythonic?
Aucun commentaire:
Enregistrer un commentaire