converting from perl: variable sized unpack

phil hunt philh at comuno.freeserve.co.uk
Thu Jul 19 07:36:49 EDT 2001


On 18 Jul 2001 21:19:54 GMT, Quinn Dunkan <quinn at yak.ugcs.caltech.edu> wrote:
>
>It's not just you, but I don't agree, and it's not just me either.
>NameErrors, KeyErrors, and IndexErrors have saved me many times.  One of the
>things I didn't like about perl is how it magically created values on
>reference, and then I had to go hunt down the bad reference that created it.
>And I often store None in dicts and lists.  It would be weird if d.has_key(x)
>could be true or false while d[x] always evaluated to None.  That means that
>d[x] is implicitly creating a None when d.has_key(x) is false,

It doesn't, it's just returning a value. Though I can see how that
might be useful, e.g:

for word in words:
   freq[word] += 1

instead of in current python:

freq = {}
for word in words:
   freq[word] = freq.get(word, 0) + 1

change to:

for word in words:
   freq[word] += 1

Note this involves:

(1) if you refer to a variable that doesn't exist, it creatres it giving
it the value None

(2) None.__getitem__() makes it into a Dictionary

(3) Dict:__getitem__() on an item not found returns None

(4) None + a number equals that number

>
>Perl has undef, and lua has nil, and in both those languages saying 'x = nil'
>is equavalent to unbinding `x'

That's not what I intend.

> (I think that's true for perl, don't remember).
>If python's None were to be consistent we should remove NameError and del and
>have all unbound names evaluate to None.

Nor that. X = None should be different than X not existing.

>And lastly, and most importantly, that's the way the language works.  Some
>languages may work differently, but many languages also work this way.  Since
>obviously no one agrees, there is no "should".  And there has to be a really
>clear "should" to justify breaking code for it.

Oh sure, I'm not suggesting changfing how Python behaves, merely suggesting
that the other way of doing it is more productive.

>For the circumstances when it's handy to have a 'default value' dict or list
>that never raises, it's easy to subclass UserDict.

Indeed. Except you'd have to say x = MyDict({'a':1, 'b':2})
instead of just x = {'a':1, 'b':2}

-- 
#===== Philip Hunt == philh at comuno.freeserve.co.uk ======#
    Herbivore: effort-free public key encryption. See:
<http://www.vision25.demon.co.uk/oss/herbivore/intro.html>
        ** First software release coming soon! **






More information about the Python-list mailing list