Re: Bug? Feature? setattr(foo, '3', 4) works!
On Dec 19, 2014, at 10:33 AM, random...@fastmail.us wrote: On Fri, Dec 19, 2014, at 07:23, Ben Finney wrote: Cem Karan cfkar...@gmail.com writes: I'd like to suggest that getattr(), setattr(), and hasattr() all be modified so that syntactically invalid statements raise SyntaxErrors. What syntactically invalid statements? The only syntactically invalid statements I see you presenting are ones that *already* raise SyntaxError. I think you mean that setting an attribute on an object should be a SyntaxError if the resulting attribute's name is not a valid identifier. But why should a valid statement produce SyntaxError? I'm −1 on such a change. And some APIs - ctypes, for example - actually require using getattr with an invalid identifier in some cases (where attribute access is used for an underlying concept with names that are usually, but not always, valid identifiers: in ctypes' case, looking up symbols from DLLs.) This is the one part I didn't know of; if ctypes requires this behavior, then it can't be changed. Dave Angel, the reason I wanted to raise a SyntaxError is because from a user's point of view they look like the same type of error. That said, you're right that for anyone trying to debug the interpreter itself raising SyntaxError would make things confusing. Regardless, because ctypes requires it, it can't be changed. I'm dropping the suggestion. Thanks, Cem Karan -- https://mail.python.org/mailman/listinfo/python-list
Bug? Feature? setattr(foo, '3', 4) works!
I'm bringing this discussion over from the python-ideas mailing list to see what people think. I accidentally discovered that the following works, at least in Python 3.4.2: class foo(object): ... pass ... setattr(foo, '3', 4) dir(foo) ['3', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] getattr(foo, '3') 4 bar = foo() dir(bar) ['3', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] getattr(bar, '3') 4 hasattr(foo, '3') True hasattr(bar, '3') True However, the following doesn't work: foo.3 File stdin, line 1 foo.3 ^ SyntaxError: invalid syntax bar.3 File stdin, line 1 bar.3 ^ SyntaxError: invalid syntax I'd like to suggest that getattr(), setattr(), and hasattr() all be modified so that syntactically invalid statements raise SyntaxErrors. In messages on python-ideas, Nick Coghlan mentioned that since a Namespace is just a dictionary, the normal error raised would be TypeError and not SyntaxError; I'd like to suggest special-casing this so that using getattr(), setattr(), and hasattr() in this way raise SyntaxError instead as I think that will be less astonishing. Thoughts? Thanks, Cem Karan -- https://mail.python.org/mailman/listinfo/python-list
Re: Bug? Feature? setattr(foo, '3', 4) works!
Cem Karan cfkar...@gmail.com writes: However, the following doesn't work: foo.3 File stdin, line 1 foo.3 ^ SyntaxError: invalid syntax bar.3 File stdin, line 1 bar.3 ^ SyntaxError: invalid syntax I'd like to suggest that getattr(), setattr(), and hasattr() all be modified so that syntactically invalid statements raise SyntaxErrors. What syntactically invalid statements? The only syntactically invalid statements I see you presenting are ones that *already* raise SyntaxError. I think you mean that setting an attribute on an object should be a SyntaxError if the resulting attribute's name is not a valid identifier. But why should a valid statement produce SyntaxError? I'm −1 on such a change. -- \ “Education is learning what you didn't even know you didn't | `\ know.” —Daniel J. Boorstin, historian, 1914–2004 | _o__) | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Bug? Feature? setattr(foo, '3', 4) works!
On 12/19/14 6:40 AM, Cem Karan wrote: I'm bringing this discussion over from the python-ideas mailing list to see what people think. I accidentally discovered that the following works, at least in Python 3.4.2: class foo(object): ... pass ... setattr(foo, '3', 4) dir(foo) ['3', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] getattr(foo, '3') 4 bar = foo() dir(bar) ['3', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] getattr(bar, '3') 4 hasattr(foo, '3') True hasattr(bar, '3') True However, the following doesn't work: foo.3 File stdin, line 1 foo.3 ^ SyntaxError: invalid syntax bar.3 File stdin, line 1 bar.3 ^ SyntaxError: invalid syntax I'd like to suggest that getattr(), setattr(), and hasattr() all be modified so that syntactically invalid statements raise SyntaxErrors. In messages on python-ideas, Nick Coghlan mentioned that since a Namespace is just a dictionary, the normal error raised would be TypeError and not SyntaxError; I'd like to suggest special-casing this so that using getattr(), setattr(), and hasattr() in this way raise SyntaxError instead as I think that will be less astonishing. Thoughts? Thanks, Cem Karan Can you explain why you think it is important that setattr(obj, '3', 3) raise an error at all? Yes, it surprised you, but there are many aspects of Python that are surprising. That's not a bad thing in and of itself. Did this cause a real problem in your code? Is there a way that allowing non-identifier attribute names will be harmful? -- Ned Batchelder, http://nedbatchelder.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Bug? Feature? setattr(foo, '3', 4) works!
On 12/19/2014 06:40 AM, Cem Karan wrote: I'm bringing this discussion over from the python-ideas mailing list to see what people think. I accidentally discovered that the following works, at least in Python 3.4.2: class foo(object): ... pass ... setattr(foo, '3', 4) dir(foo) ['3', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] getattr(foo, '3') snip However, the following doesn't work: foo.3 File stdin, line 1 foo.3 ^ SyntaxError: invalid syntax bar.3 File stdin, line 1 bar.3 ^ SyntaxError: invalid syntax I'd like to suggest that getattr(), setattr(), and hasattr() all be modified so that syntactically invalid statements raise SyntaxErrors. In messages on python-ideas, Nick Coghlan mentioned that since a Namespace is just a dictionary, the normal error raised would be TypeError and not SyntaxError; I'd like to suggest special-casing this so that using getattr(), setattr(), and hasattr() in this way raise SyntaxError instead as I think that will be less astonishing. They are NOT syntactically invalid, but perhaps you mean they should be considered semantically invalid. That's a very important distinction. Suppose for example the function call is setattr(foo, name, value) If the setattr() call is made inside a function, there's no way that the compiler could detect it as a syntax error. IF YOU decide it should be a runtime error, then labelling it as a syntax error would be very confusing to anyone who understands the compile process. I personally don't think it should be an error at all, and that current behavior is just fine. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: Bug? Feature? setattr(foo, '3', 4) works!
On 12/19/2014 6:40 AM, Cem Karan wrote: I'm bringing this discussion over from the python-ideas mailing list to see what people think. I accidentally discovered that the following works, at least in Python 3.4.2: class foo(object): ... pass ... setattr(foo, '3', 4) dir(foo) ['3', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] getattr(foo, '3') 4 bar = foo() dir(bar) ['3', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] getattr(bar, '3') 4 hasattr(foo, '3') True hasattr(bar, '3') True The fact that one has set and retrieve non-identifier attributes has always been true and known and discussed before. Guido has declared that it should continue to work rather than break code that uses this fact (and their is apparently such). This follows under Python's 'consenting adults' policy. The attribute 'name' is type-checked to be a string. class C: pass c = C() setattr(c, 3, '3') Traceback (most recent call last): File pyshell#3, line 1, in module setattr(c, 3, '3') TypeError: attribute name must be string, not 'int' setattr(c, b'3', '3') Traceback (most recent call last): File pyshell#4, line 1, in module setattr(c, b'3', '3') TypeError: attribute name must be string, not 'bytes' If one accesses the dict directly, even the string limitation is bypassed, but one suffers the consequences. c.__dict__[3] = 3 dir(c) Traceback (most recent call last): File pyshell#9, line 1, in module dir(c) TypeError: unorderable types: int() str() Thoughts? Don't pursue this. Leave good enough alone. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Bug? Feature? setattr(foo, '3', 4) works!
On Fri, Dec 19, 2014, at 07:23, Ben Finney wrote: Cem Karan cfkar...@gmail.com writes: I'd like to suggest that getattr(), setattr(), and hasattr() all be modified so that syntactically invalid statements raise SyntaxErrors. What syntactically invalid statements? The only syntactically invalid statements I see you presenting are ones that *already* raise SyntaxError. I think you mean that setting an attribute on an object should be a SyntaxError if the resulting attribute's name is not a valid identifier. But why should a valid statement produce SyntaxError? I'm −1 on such a change. And some APIs - ctypes, for example - actually require using getattr with an invalid identifier in some cases (where attribute access is used for an underlying concept with names that are usually, but not always, valid identifiers: in ctypes' case, looking up symbols from DLLs.) -- https://mail.python.org/mailman/listinfo/python-list