[Python-Dev] Other library code transformations

Guido van Rossum guido@python.org
Fri, 31 May 2002 12:24:05 -0400


> While we're eliminating uses of the string and types modules, how about
> other code clean-ups and modernization:

In general I'd only do these when you're working on a module anyway.
And again stay out of the larger packages altogether.

> d.has_key(k) --> k in d
> if k in d.keys() --> if k in d

OK.

> obj.__dict__ --> vars(obj)

No.  vars() exists in the first place to be called without arguments,
to look in the interactive namespace.

> class X(UserList) --> class X(list)

That would be a huge change in semantics.

> class X(UserDict) --> class X(dict)  # and remove .data references

Ditto.

> 0L --> 0
> 1L --> 1

Careful -- there are still semantic differences (e.g. 1<<100 == 0,
1L<<100 == 2**100).

> lambda x, y=y: fun(x,y) --> lambda x: fun(x,y)

This changes semantics!  If the outer y is later assigned to, the
nested-scopes form tracks that change, the default arg doesn't.

> x = y.__class__ --> x is type(y)

These are not the same!  A metaclass can override __class__ but not
type(); also, for a classic class instance type() and __class__ have
different values.

> == None --> is None

Yes!

> if item in astring --> if item in adict

Huh?  What does this mean?

> This is from my own list of code updates, the python library already
> in better shape.

I'd rather see effort go towards other things.  Have you looked at
IDLEFORK yet?

--Guido van Rossum (home page: http://www.python.org/~guido/)