
Is it possible to signal to a parameter to use its default? def f( a, b=None, c='' ):... f( 123, <def>, 'abc' ) f( 123, ObjA, <def> ) Did I miss something, plus did you cover it before?

Actually, I wanted a uniform way to call f. f(a,b,c) for both cases if b can equal <def>. -----Original Message----- From: Steven Bethard [mailto:steven.bethard@gmail.com] Sent: Wednesday, May 09, 2007 3:45 PM To: Aaron Brady Cc: python-ideas@python.org Subject: Re: [Python-ideas] parameter omit On 5/9/07, Aaron Brady <castironpi@comcast.net> wrote:
In this case, it's pretty easy:: f(123, c='abc') f(123, ObjA) STeVe -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy

No, still not uniform. Cases might be rare, even syntactic sugar maybe. if something huge: b=<def> more huge if something else: b=mything still more f(a,b,c). -----Original Message----- From: Josiah Carlson [mailto:jcarlson@uci.edu] Sent: Wednesday, May 09, 2007 4:51 PM To: Aaron Brady; python-ideas@python.org Subject: Re: [Python-ideas] parameter omit "Aaron Brady" <castironpi@comcast.net> wrote:
Actually, I wanted a uniform way to call f. f(a,b,c) for both cases if b can equal <def>.
f(a, b=...) #default c f(a, c=...) #default b - Josiah

You can almost do, b=f.func_defaults[1], but you still have to know where the defaults start. Very small what I'm missing. -----Original Message----- From: python-ideas-bounces@python.org [mailto:python-ideas-bounces@python.org] On Behalf Of Aaron Brady Sent: Wednesday, May 09, 2007 4:50 PM To: 'Josiah Carlson'; python-ideas@python.org Subject: Re: [Python-ideas] parameter omit No, still not uniform. Cases might be rare, even syntactic sugar maybe. if something huge: b=<def> more huge if something else: b=mything still more f(a,b,c). -----Original Message----- From: Josiah Carlson [mailto:jcarlson@uci.edu] Sent: Wednesday, May 09, 2007 4:51 PM To: Aaron Brady; python-ideas@python.org Subject: Re: [Python-ideas] parameter omit "Aaron Brady" <castironpi@comcast.net> wrote:
Actually, I wanted a uniform way to call f. f(a,b,c) for both cases if b can equal <def>.
f(a, b=...) #default c f(a, c=...) #default b - Josiah
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

Aaron Brady wrote:
You might be able to make a decorator that handles this the way you describe. It might look something like the following. class DefaultValue(object): """ An signal object. """ pass DfV = DefaultValue() # Default value signal object def defaults(*args): " a decorator that assigns and checks defaults values." ... @defaults(None, None, '') def f(a, b, c): return a, b, c Then you could do as you wrote above and get something like...
f(123, DfV, 'abc') 123, None, 'abc'
f(123, ObjA, DfV) 123, <OjbA>, ''
I'll leave the actual implementation to you. It might be useful in cases where you want the calling signature to look alike for a group of dispatched functions and the added overhead the decorator adds isn't a problem. But you probably wouldn't want that overhead all the time, so having it as an optional decorator would be good. Cheers, Ron

Great ideas. It turned out really nicely. class GuardDefault: Val= object() def __call__( self, *args, **kwargs ): args=list(args) for i,j in enumerate( args ): if j is GuardDefault.Val: offset= self.callable.func_code.co_argcount-\ len(self.callable.func_defaults) args[i]= self.callable.func_defaults[i-offset] return self.callable( *args,**kwargs ) def __init__( self,callable ): self.callable= callable @GuardDefault def f( a,b=None,c='abc' ): print a,b,c f( 0,GuardDefault.Val,'def' ) f( 0,'somebody',GuardDefault.Val ) gives: 0 None def 0 somebody abc

Actually, I wanted a uniform way to call f. f(a,b,c) for both cases if b can equal <def>. -----Original Message----- From: Steven Bethard [mailto:steven.bethard@gmail.com] Sent: Wednesday, May 09, 2007 3:45 PM To: Aaron Brady Cc: python-ideas@python.org Subject: Re: [Python-ideas] parameter omit On 5/9/07, Aaron Brady <castironpi@comcast.net> wrote:
In this case, it's pretty easy:: f(123, c='abc') f(123, ObjA) STeVe -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy

No, still not uniform. Cases might be rare, even syntactic sugar maybe. if something huge: b=<def> more huge if something else: b=mything still more f(a,b,c). -----Original Message----- From: Josiah Carlson [mailto:jcarlson@uci.edu] Sent: Wednesday, May 09, 2007 4:51 PM To: Aaron Brady; python-ideas@python.org Subject: Re: [Python-ideas] parameter omit "Aaron Brady" <castironpi@comcast.net> wrote:
Actually, I wanted a uniform way to call f. f(a,b,c) for both cases if b can equal <def>.
f(a, b=...) #default c f(a, c=...) #default b - Josiah

You can almost do, b=f.func_defaults[1], but you still have to know where the defaults start. Very small what I'm missing. -----Original Message----- From: python-ideas-bounces@python.org [mailto:python-ideas-bounces@python.org] On Behalf Of Aaron Brady Sent: Wednesday, May 09, 2007 4:50 PM To: 'Josiah Carlson'; python-ideas@python.org Subject: Re: [Python-ideas] parameter omit No, still not uniform. Cases might be rare, even syntactic sugar maybe. if something huge: b=<def> more huge if something else: b=mything still more f(a,b,c). -----Original Message----- From: Josiah Carlson [mailto:jcarlson@uci.edu] Sent: Wednesday, May 09, 2007 4:51 PM To: Aaron Brady; python-ideas@python.org Subject: Re: [Python-ideas] parameter omit "Aaron Brady" <castironpi@comcast.net> wrote:
Actually, I wanted a uniform way to call f. f(a,b,c) for both cases if b can equal <def>.
f(a, b=...) #default c f(a, c=...) #default b - Josiah
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

Aaron Brady wrote:
You might be able to make a decorator that handles this the way you describe. It might look something like the following. class DefaultValue(object): """ An signal object. """ pass DfV = DefaultValue() # Default value signal object def defaults(*args): " a decorator that assigns and checks defaults values." ... @defaults(None, None, '') def f(a, b, c): return a, b, c Then you could do as you wrote above and get something like...
f(123, DfV, 'abc') 123, None, 'abc'
f(123, ObjA, DfV) 123, <OjbA>, ''
I'll leave the actual implementation to you. It might be useful in cases where you want the calling signature to look alike for a group of dispatched functions and the added overhead the decorator adds isn't a problem. But you probably wouldn't want that overhead all the time, so having it as an optional decorator would be good. Cheers, Ron

Great ideas. It turned out really nicely. class GuardDefault: Val= object() def __call__( self, *args, **kwargs ): args=list(args) for i,j in enumerate( args ): if j is GuardDefault.Val: offset= self.callable.func_code.co_argcount-\ len(self.callable.func_defaults) args[i]= self.callable.func_defaults[i-offset] return self.callable( *args,**kwargs ) def __init__( self,callable ): self.callable= callable @GuardDefault def f( a,b=None,c='abc' ): print a,b,c f( 0,GuardDefault.Val,'def' ) f( 0,'somebody',GuardDefault.Val ) gives: 0 None def 0 somebody abc
participants (5)
-
Aaron Brady
-
Josiah Carlson
-
Ron Adam
-
Scott Dial
-
Steven Bethard