Is there are good DRY fix for this painful design pattern?

Jugurtha Hadjar jugurtha.hadjar at gmail.com
Thu Mar 1 07:56:38 EST 2018


On 03/01/2018 10:34 AM, Gregory Ewing wrote:
> After this thread, the term "docstring" is never going
> to mean quite the same thing to me again.
>
I still feel that the following is quite readable:
<----------------------------------->
import inspect
def snatch(func):
     def snatched(self, *args, **kwargs):
         signature = inspect.signature(func)
         for name in signature.parameters:
             if name != 'self':
                 kwargs.setdefault(name, getattr(self, name))
         return func(self, *args, **kwargs)
     return snatched
class Foo(object):
     def __init__(self, sleepy, groggy):
         self.sleepy = sleepy
         self.groggy = groggy
     @snatch
     def spam(self, sleepy=None):
         print("I am spam. sleepy=", sleepy)
     @snatch
     def ham(self, sleepy=None, groggy=None):
         print("I am ham. sleepy=", sleepy, " and groggy=", groggy)
<-------------------------------------->
-- 
~ Jugurtha Hadjar,


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /