Ich schlage vor, function_4 unten zusammen mit den Fragen angezeigt (nicht idetnically arbeiten!) Funktionen und einer von DomTomCat Antwort:
#! /usr/bin/env python
from __future__ import print_function
from collections import OrderedDict # Only used in function_3
def function_4(self):
"""Iterate over call results in FIFO on False or if sequence
exhausted, return None or previous value if that evaluates to true."""
functors = (
self.get_a,
self.get_b,
self.get_c,
)
request_targets = (
'a',
'b',
'c',
)
response_value = None
for functor, request_target in zip(functors, request_targets):
current_response = functor()
if not current_response:
print(request_target, "not set.")
return response_value
else:
response_value = current_response
return response_value
class Foo(object):
"""Mock the thingy ..."""
def __init__(self, a, b, c):
self._a, self._b, self._c = a, b, c
def __repr__(self):
return (
"Foo(" + str(self._a) + ", " + str(self._b) + ", " +
str(self._c) + ")")
def get_a(self):
return self._a
def get_b(self):
return self._b
def get_c(self):
return self._c
def function_1(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 function_2(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
def function_3(self):
my_dictionary = OrderedDict()
my_dictionary['a'] = self.get_a()
my_dictionary['b'] = self.get_b()
my_dictionary['c'] = self.get_c()
# ...
r = None
for name in my_dictionary.keys():
value = my_dictionary[name]
if not value:
print("%s not set." % name)
return r
r = value
def main():
""""Drive the investigation."""
fixtures = (
(1, 42, 3.1415),
(0, 42, 3.1415),
(1, 0, 3.1415),
(1, 42, 0),
)
functors = (
function_1,
function_2,
function_3,
function_4,
)
for fixture in fixtures:
foo = Foo(*fixture)
print("\nFixture:", foo)
for i, functor in enumerate(functors, start=1):
print("Functor[%d]:" % (i,))
print(functor(foo))
if __name__ == '__main__':
main()
Auf meinem Rechner die Leuchten erzeugen das folgende Verhalten/Ausgang, wenn sie aufgerufen werden:
Fixture: Foo(1, 42, 3.1415)
Functor[1]:
3.1415
Functor[2]:
3.1415
Functor[3]:
None
Functor[4]:
3.1415
Fixture: Foo(0, 42, 3.1415)
Functor[1]:
a not set.
None
Functor[2]:
a not set.
None
Functor[3]:
a not set.
None
Functor[4]:
a not set.
None
Fixture: Foo(1, 0, 3.1415)
Functor[1]:
b not set.
1
Functor[2]:
b not set.
1
Functor[3]:
b not set.
1
Functor[4]:
b not set.
1
Fixture: Foo(1, 42, 0)
Functor[1]:
c not set.
42
Functor[2]:
c not set.
0
Functor[3]:
c not set.
42
Functor[4]:
c not set.
42
[Finished in 0.0s]
Sie können auch 'r' loswerden und direkt' a', 'b' oder' c' zurückgeben. – Selcuk
Wenn Sie die 'print'-Aufrufe in den' else'-Zweigen nicht benötigen, können Sie die gesamte Berechnung auf einmal durchführen, mit 'a und (b und c oder b) oder a oder None'. Das 'oder None' am Ende ist nur dann notwendig, wenn die möglichen" falsey "-Werte, auf die Sie testen, nicht auf None beschränkt sind (und es ist Ihnen wichtig," None "anstatt einer anderen falschen Sache zurückzugeben). – Blckknght
@Blckknght Ich habe deinen Kommentar nicht gesehen, bevor ich ihn eingereicht habe. Wenn du ihn als Antwort einreichen willst, werde ich meinen löschen. –