Permanent objects?

Kevin Altis altis at semi-retired.com
Wed Dec 25 02:58:10 EST 2002


"Tim Peters" <tim.one at comcast.net> wrote in message
news:mailman.1040775931.29528.python-list at python.org...
> [Tim]
> >> For example, never use "is" to compare immutable objects, unless you
> >> don't care what the result is <wink>.  The optimizations can (and do)
> >> change across Python releases.
>
> [Dave Brueck]
> > Does this also apply to None?
>
> Yes, provided you supply the missing paragraph <wink> about
> guaranteed-singleton objects, where "is" is fine.  None and type objects
are
> guaranteed unique, so "x is None" and "type(d) is dict" are fine (and
> idiomatic).  No guarantees about uniqueness are made wrt ints, longs,
> floats, complexes, strings, Unicode strings, or tuples.  Uniqueness is
> guaranteed for True and False in Python 2.3.

On a related note, I would like to confirm that the preferred check for type
is to use the built-in type factories rather than the types module?

>>> type('hello') is str
1
>>> type(u'hello') in (str, unicode)
1
>>> type(['world']) is list
1

instead of

>>> import types
>>> type('hello') is types.StringType
1
>>> type(u'hello') in types.StringTypes
1
>>> type(['world']) is types.ListType
1

There are some objects that are a bit trickier like functions and modules

>>> def f():
...     pass
...
>>> callable(f)
1
>>> type(f) is types.FunctionType
1

Of course, if the types module isn't going away or getting deprecated
anytime soon I guess the old style is perfectly fine, but is it recommended?
I seem to remember Guido not liking types.StringType ... I'm doing some
holiday refactoring of old code.

Also, now that I did some searches, I'm finding a variety of variables in my
code named 'list' and I suppose I should be changing those, even though
'list' isn't reserved and using it as a variable name probably only impacts
code in the same scope that I used the variable so as long as I don't try
and do an "is list" check or list() conversion I'm okay.

ka





More information about the Python-list mailing list