Will python never intend to support private, protected and public?

Bengt Richter bokr at oz.net
Mon Oct 3 11:29:36 EDT 2005


On 03 Oct 2005 04:47:26 -0700, Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

>bokr at oz.net (Bengt Richter) writes:
>> Would you want to outlaw 'None' as an attribute name?
>> Python seems to be straddling the fence at this point:
>>  >>> c.None = 'c.None'
>>  SyntaxError: assignment to None
>
>Heehee, I think that's just a compiler artifact, the lexer is treating
>None as a keyword instead of a normal lexical symbol that the compiler
>treats separately.  That's also why it raises SyntaxError instead of
>some other type of error.  Yes, None should be ok as an attribute name.
>
Not sure whether which compiler. This one seems to differ from the C version.

 >>> import compiler
 >>> compiler.parse("c.None = 'c.None'")
 Module(None, Stmt([Assign([AssAttr(Name('c'), 'None', 'OP_ASSIGN')], Const('c.None'))]))

 >>> compiler.compile("c.None = 'c.None'", '', 'exec')
 <code object <module> at 02EE7FA0, file "", line 1>
 >>> import dis
 >>> dis.dis(compiler.compile("c.None = 'c.None'", '', 'exec'))
   1           0 LOAD_CONST               1 ('c.None')
               3 LOAD_NAME                0 (c)
               6 STORE_ATTR               1 (None)
               9 LOAD_CONST               0 (None)
              12 RETURN_VALUE
 >>> c = type('',(),{})()
 >>> exec   (compiler.compile("c.None = 'c.None'", '', 'exec'))
 >>> c.None
 'c.None'

So the compiler module is happy to generate code that you can execute,
but the builtin compiler seems not to be:

 >>> c.None = 'c.None'
 SyntaxError: assignment to None

and definitely not run-time:
 >>> def foo():
 ...     c.None = 'c.None'
 ...
   File "<stdin>", line 2
 SyntaxError: assignment to None

Seems like a bug wrt the intent of making compiler.compile work
exactly like the builtin C version. But maybe it has been fixed --
I am still running 2.4 from the beta I built with mingw (because
the new microsoft msi loader won't run on my version of NT4 without
upgrading that I've got too much dll hell to do on this box.
I should also upgrade mingw/msys and recompile, but I spend time here instead ;-/ )

Python 2.4b1 (#56, Nov  3 2004, 01:47:27)
[GCC 3.2.3 (mingw special 20030504-1)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Regards,
Bengt Richter



More information about the Python-list mailing list