[Python-3000] Spooky behavior of dict.items() and friends

"Martin v. Löwis" martin at v.loewis.de
Wed Apr 2 21:37:19 CEST 2008


> Is there a particular rationale describing the use of function calls  
> vs. object properties in core Python?
> 
> When I see a function call required for something that could be  
> conveniently expressed as a property, it generally tells me "I'm  
> computing something. It might be expensive, and if you call me again,  
> I'll have to recompute."
> 
> This made sense with .keys() in 2.x, but is not true in 3.0. Is there  
> a good reason besides compatibility to keep the parentheses there?
> 
>      sorted(x.keys)
> 
> has a nice ring to it. Cheers,

In the current implementation (3.0a3+),

x.keys() is not x.keys()

i.e. it does indeed recompute something new each time.

Now, the objects it creates have the same state, so it technically
wouldn't have to create a fresh object each time. One issue is that
by doing so, you prevent cyclic references (the dict actually doesn't
need to know what views of it exist), so this works better for
garbage collection.

Implementation issues aside, I presume that the choice of interface
primarily comes from tradition.

Regards,
Martin


More information about the Python-3000 mailing list