concept of creating structures in python

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Dec 12 17:54:18 EST 2008


On Fri, 12 Dec 2008 09:07:21 -0700, Joe Strout wrote:

>> Joe missed a piece out here. If you change the signature of your
>> D.__init__() to read
>>
>>     def  __init__(self, dataName='ND', index = 0, ele_obj=None):
>>
>> then you need to insert the following code at the top of the method:
>>
>>        if ele_obj is None:
>>            ele_obj = E()
> 
> Yes, if you really need to guarantee that ele_obj is not None, then this
> is the way to do it.
> 
> Of course this would mean that you can't get a None ele_obj even by
> passing it in explicitly as the parameter value -- if you need to
> support that as well, then the solution is a little more complex.
> Perhaps best in that case would be to just not give ele_obj any default
> value at all, so it becomes a required parameter.


Just use a sentinel that isn't None.


class D(...):
    sentinel = object()
    def __init__(self, dataName='ND', index = 0, ele_obj=sentinel):
        if ele_obj is D.sentinel:
            ...


-- 
Steven



More information about the Python-list mailing list