__iadd__ and fellows missing (Python 2.2)
Bengt Richter
bokr at oz.net
Sat Apr 13 20:30:52 EDT 2002
On Sat, 13 Apr 2002 21:01:41 GMT, "Terry Reedy" <tejarex at yahoo.com> wrote:
>
>"Mike C. Fletcher" <mcfletch at rogers.com> wrote in message
>news:mailman.1018723227.15055.python-list at python.org...
>> I was unaware that there was no guarantee for 0-length tuples always
>> being unique objects. There are lots of places in my code where I
>use
>> 0-length tuples in this pattern:
>>
>> NULL = ()
>>
>> class A:
>> def b( self, object=NULL ):
>> if object is NULL:
>> #was not specified
>> elif object is None:
>> #specified to be object None
>>
>> Of course, I can use:
>>
>> NULL = []
>>
>> But I'd always avoided that because of bad associations with having
>> mutable objects as default parameters (yes, it doesn't actually
>matter
>> in this pattern, but it's a thirty-times-burned-thirty-first-shy
>thing).
>> Suppose I'll wind up using singleton instances instead.
>
>Given that None is a possible input, so it cannot be used as the
>no-input flag, you could also try something like NULL="OpTioNaL ArG
>NoT GiVeN"
>
You could also do:
>>> class A:
... def b(self, *obj):
... if not obj: return 'obj not specified'
... return 'did something with "%s"' % (obj[0],)
...
>>> a=A()
>>> a.b()
'obj not specified'
>>> for x in [None,(),[],{},'',0]: print a.b(x)
...
did something with "None"
did something with "()"
did something with "[]"
did something with "{}"
did something with ""
did something with "0"
Regards,
Bengt Richter
More information about the Python-list
mailing list