New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
namedtuple: raise an exception when the given class name would override an existing name #66753
Comments
The nametuple function creates classes dynamically. The caller must provide the class name thus overriding any existing object name. While this behavior is not new to Python, I am unaware of a simple function with such an awkward side effect. Consequently, the caller would have to delete the object under that The code example given below shows the survival of In [1]: from collections import namedtuple In [2]: AB=namedtuple('AB', ('a','b')) In [3]: ab=AB(4,9) In [4]: type(ab) In [6]: AB=namedtuple('AB', ('c','d')) In [7]: type(ab) In [8]: ab2=AB(16,25) In [9]: type(ab2) In [10]: type(ab) In [11]: ab In [12]: ab2 |
IMO, this is a horrible idea. I'd hate for creation of a I also think this would be pretty much impossible to implement efficiently. :-) I'm closing this report here: this behaviour is not a bug, and the suggested exception raising is (IMO) both undesirable and unworkable in practice. I'd suggest opening a thread on the Python mailing-list if you want to discuss the idea further; if that thread produces a focused, widely accepted proposal, feel free to re-open an issue here. |
Perhaps Leo doesn’t understand that the name passed to “namedtuple” is just an indicator for debugging etc, and it doesn’t really have to be unique or even correspond with what it is assigned to. I do remember finding it a bit odd that I had to give it a name when I first used “namedtuple”, but I guess it is because all Python function and class objects store their name internally. >>> AB = namedtuple("Whatever", ("a", "b"))
>>> AB
<class '__main__.Whatever'>
>>> Whatever
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Whatever' is not defined
>>> Whatever = AB
>>> Whatever
<class '__main__.Whatever'> |
Right; the underlying problem of having objects that appear to be instances of the wrong class has nothing to do with namedtuples. After this sequence: >>> class A: pass
...
>>> a = A()
>>>
>>> class A: pass
... We get the following somewhat confusing results: >>> type(a)
<class '__main__.A'>
>>> A
<class '__main__.A'>
>>> isinstance(a, A)
False Class factories like namedtuple make it somewhat easier to run into this issue, but it's nothing really to do with namedtuple itself, and it's not something that could be "fixed" without significant language redesign. |
The idea was based on a misunderstanding of the typename argument. I I agree that the oddities shown in the two code examples are just an Rereading the docs on nametuple, I think a clarification on the Thanks for the helpful feedback. Leo On 06/10/2014, Mark Dickinson <report@bugs.python.org> wrote:
>
> Mark Dickinson added the comment:
>
> Right; the underlying problem of having objects that appear to be instances
> of the wrong class has nothing to do with namedtuples. After this
> sequence:
>
>>>> class A: pass
> ...
>>>> a = A()
>>>>
>>>> class A: pass
> ...
>
> We get the following somewhat confusing results:
>
>>>> type(a)
> <class '__main__.A'>
>>>> A
> <class '__main__.A'>
>>>> isinstance(a, A)
> False
>
> Class factories like namedtuple make it somewhat easier to run into this
> issue, but it's nothing really to do with namedtuple itself, and it's not
> something that could be "fixed" without significant language redesign.
>
>
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: