How Can I Use A Static Method As A Default Parameter For The Strategy Design Pattern?
Solution 1:
No, you cannot, because the class
definition has not yet completed running so the class name doesn't exist yet in the current namespace.
You can use the function object directly:
classC: @staticmethoddefdefault_concrete_strategy():
print("default")
@staticmethoddefother_concrete_strategy():
print("other")
def__init__(self, strategy=default_concrete_strategy.__func__):
self.strategy = strategy
C
doesn't exist yet when the methods are being defined, so you refer to default_concrete_strategy
by the local name. .__func__
unwraps the staticmethod
descriptor to access the underlying original function (a staticmethod
descriptor is not itself callable).
Another approach would be to use a sentinel default; None
would work fine here since all normal values for strategy
are static functions:
classC:
@staticmethoddefdefault_concrete_strategy():
print("default")
@staticmethoddefother_concrete_strategy():
print("other")
def__init__(self, strategy=None):
if strategy isNone:
strategy = self.default_concrete_strategy
self.strategy = strategy
Since this retrieves default_concrete_strategy
from self
the descriptor protocol is invoked and the (unbound) function is returned by the staticmethod
descriptor itself, well after the class definition has completed.
Post a Comment for "How Can I Use A Static Method As A Default Parameter For The Strategy Design Pattern?"