concept of creating structures in python

Steve Holden steve at holdenweb.com
Fri Dec 12 11:55:27 EST 2008


Joe Strout wrote:
> On Dec 12, 2008, at 9:00 AM, Steve Holden wrote:
> 
>>> Change the default value of ds_obj here to None.  Otherwise, you will
>>> certainly confuse yourself (there would be just one default object
>>> shared among all instances).
>>>
>> 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 for completeness, if you want to be able to pass None then you need
to create a sentinel object, usually just an instantiation of object(),
and test for identity with that to determine that no argument was provided.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list