DRY functions with named attributes used as default arguments

Tim Chase python.list at tim.thechases.com
Sun Oct 9 08:20:45 EDT 2011


My intent is to have a function object something like
 def foo(arg1, arg2=foo.DEFAULT):
 return int(do_stuff(arg1, arg2))
 foo.SPECIAL = 42
 foo.MONKEY = 31415
 foo.DEFAULT = foo.SPECIAL
so I can call it with either
 result = foo(myarg)
or
 result = foo(myarg, foo.SPECIAL)
However I can't do this because foo.DEFAULT isn't defined at the 
time the function is created. I'd like to avoid hard-coding 
things while staying DRY, so I don't like
 def foo(arg1, arg2=42)
because the default might change due to business rule changes, I 
have a dangling "magic constant" and if the value of SPECIAL 
changes, I have to catch that it should be changed in two places.
My current hack/abuse is to use __new__ in a class that can 
contain the information:
 class foo(object):
 SPECIAL = 42
 MONKEY = 31415
 DEFAULT = SPECIAL
 def __new__(cls, arg1, arg2=DEFAULT):
 return int(do_stuff(arg1, arg2))
 i1 = foo("spatula")
 i2 = foo("tapioca", foo.MONKEY)
1) is this "icky" (a term of art ;-)
2) or is this reasonable
3) or is there a better way to do what I want?
Thanks,
-tkc


More information about the Python-list mailing list

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