[Python-ideas] Bug? Feature? setattr(foo, '3', 4) works!
rosuav at gmail.com
Fri Dec 19 06:15:13 CET 2014
On Fri, Dec 19, 2014 at 3:27 PM, Cem Karan <cfkaran2 at gmail.com> wrote:
> I'd like to suggest that getattr(), setattr(), and hasattr() all be modified so that syntactically invalid statements raise SyntaxErrors.
Is there actually a need to have them check like that? :) After all,
attributes are just stored in a dictionary. You want
syntactically-invalid attributes? Sure, no problem.
> On a related note, are numbers defined in a module, or they are part of interpreter? Just to see how awful I could make things, I tried to extend the above to redefine 3 to be 4. Fortunately (unfortunately?) I couldn't find a way, but I'm curious if there is a pythonic way of making this happen, and if there is, I'd like to suggest making that impossible ASAP.
Syntax comes first. If you want to play with redefinitions like that,
it'd be simpler to stuff stuff into globals(), which is also simply a
When you type 3, Python compiles that into the integer 3, not into a
name lookup. Now, you *can* fiddle with this kind of thing using
ctypes, though I don't know how many other interpreters (PyPy, Jython,
etc) will let you shoot yourself in that particular foot. But if you'd
like to play around with this kind of thing, check out the 'ast'
module and try compiling code to AST, then fiddling with it a bit,
then finishing compilation and execution. And python-list will
probably enjoy discussing the random things you can accomplish with
that :) It's good fun.
More information about the Python-ideas