Use cases for del
Grant Edwards
grante at visi.com
Wed Jul 6 21:46:48 EDT 2005
On 2005-07-07, Ron Adam <rrr at ronadam.com> wrote:
> Grant Edwards wrote:
>
>> On 2005-07-06, Ron Adam <rrr at ronadam.com> wrote:
>>
>>
>>>It would be a way to set an argument as being optional without
>>>actually assigning a value to it. The conflict would be if
>>>there where a global with the name baz as well. Probably it
>>>would be better to use a valid null value for what ever baz if
>>>for. If it's a string then "", if its a number then 0, if it's
>>>a list then [], etc...
>>
>> Except those aren't "null values" for those types. 0 is a
>> perfectly good integer value, and I use it quite often. There's
>> a big difference between an "invalid integer value" and an
>> integer with value 0.
>
> Why would you want to use None as an integer value?
1) So I know whether an parameter was passed in or not. Perhaps
it's not considered good Pythonic style, but I like to use a
single method for both get and set operations. With no
parameters, it's a get. With a parameter, it's a set:
class demo:
def foo(v=None):
if v is not None:
self.v = v
return self.v
2) So I can use it as sort of a NaN equivalent.
if self.fd is None:
self.fd = os.open('foo.bar','w')
if self.fd is not None:
os.close(self.fd)
self.fd = None
> If a value isn't established yet, then do you need the name
> defined?
I find it more obvious to set the name to None during the
periods that it isn't valid than to delete it and check for a
NameError when I want to know if the value is usable or not.
> Wouldn't it be better to wait until you need the name then
> give it a value?
"Better" is a value judgement. I prefer setting it None and
than deleting it and then checking for existance.
--
Grant Edwards grante Yow! Hey, LOOK!! A pair of
at SIZE 9 CAPRI PANTS!! They
visi.com probably belong to SAMMY
DAVIS, JR.!!
More information about the Python-list
mailing list