Using fixed variables probably doesn't help
Andrew Bennetts
andrew at canonical.com
Fri Jun 16 06:34:12 BST 2006
On Thu, Jun 15, 2006 at 08:57:44PM -0500, John Arbash Meinel wrote:
[...]
> But the really interesting thing to me, is that it seems static strings
> are always interned. So we don't need to create a separate object for them.
More specifically, string constants in your code will be interned if they look
like python identifiers:
>>> x = 'a b'
>>> y = 'a b'
>>> id(x), id(y)
(-1210236576, -1210236608)
> I'm not sure what the exact criteria is, when I tried a really long
> string it would seem that the first time didn't intern the string, but
> the second time would. (The id('longstring') would change twice, and
> then become stable).
The actual check is all_name_chars in Objects/codeobject.c, and it simply checks
that all characters in the string are in the set
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz". So a literal
"111" would be interned, even though it's not a valid python identifier.
> Anyway, I'm guessing that we shouldn't spend any time trying to pass
> around object references rather than just using 'file' and 'directory'.
> The only thing we may want to start doing is calling 'intern()' for
> stuff we read from a file.
The main benefit to using variables rather than constants is that static
analysis tools like pychecker have a much better chance of noticing that:
some_func(kind=consts.dircetory, ...)
has a typo than:
some_func(kind="dircetory", ...)
-Andrew.
More information about the bazaar
mailing list