Skip to content
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

Adding __getitem__ as a class method doesn't work as expected #59494

Closed
samwyse mannequin opened this issue Jul 7, 2012 · 3 comments
Closed

Adding __getitem__ as a class method doesn't work as expected #59494

samwyse mannequin opened this issue Jul 7, 2012 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@samwyse
Copy link
Mannequin

samwyse mannequin commented Jul 7, 2012

BPO 15289
Nosy @ericsnowcurrently
Files
  • 20120607.py: demonstrates the behavior
  • Issue15289.py: implements the desired behavior
  • 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:

    assignee = None
    closed_at = <Date 2012-07-07.20:37:42.567>
    created_at = <Date 2012-07-07.19:47:57.289>
    labels = ['interpreter-core', 'type-bug', 'invalid']
    title = "Adding __getitem__ as a class method doesn't work as expected"
    updated_at = <Date 2012-07-10.14:48:09.114>
    user = 'https://bugs.python.org/samwyse'

    bugs.python.org fields:

    activity = <Date 2012-07-10.14:48:09.114>
    actor = 'samwyse'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-07-07.20:37:42.567>
    closer = 'eric.snow'
    components = ['Interpreter Core']
    creation = <Date 2012-07-07.19:47:57.289>
    creator = 'samwyse'
    dependencies = []
    files = ['26309', '26343']
    hgrepos = []
    issue_num = 15289
    keywords = []
    message_count = 3.0
    messages = ['164926', '164935', '165192']
    nosy_count = 2.0
    nosy_names = ['samwyse', 'eric.snow']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue15289'
    versions = ['Python 2.7']

    @samwyse
    Copy link
    Mannequin Author

    samwyse mannequin commented Jul 7, 2012

    I'm using a class as a decorator, and saving information in a class variable. I wanted to access the information via a __getitem__ class method, but using conventional syntax doesn't work on a class. The following exception is thrown when the attached script is run.

    Traceback (most recent call last):
      File "/Users/sam/Documents/forth.py", line 34, in <module>
        print Alias["f'"]
    TypeError: 'type' object has no attribute '__getitem__'

    @samwyse samwyse mannequin added the type-bug An unexpected behavior, bug, or error label Jul 7, 2012
    @ericsnowcurrently
    Copy link
    Member

    This appears to be a misunderstanding about special-method lookup which, honestly, isn't super obvious at first. It helps to remember that classes are objects like everything else in Python and thus are instances of some type (their metaclass).

    Anyway, here's the key point regarding special-method lookup. As far as Python is concerned, the following are equivalent:

      value = obj[key]

    and

      value = type(obj).__getitem__(obj, key)

    Thus Alias()["f'"] will give you your answer, but Alias["f'"] tries to call type(Alias).__getitem__(Alias, key). Since Alias is a class, it is an instance of the built-in type class, so type(alias) is type, which does not have a __getitem__() method.

    Check out the following resources:

    http://docs.python.org/py3k/reference/datamodel.html#special-method-names
    http://docs.python.org/py3k/library/inspect.html#fetching-attributes-statically

    @ericsnowcurrently ericsnowcurrently added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 7, 2012
    @samwyse
    Copy link
    Mannequin Author

    samwyse mannequin commented Jul 10, 2012

    Thanks, Eric. Based on what you said, I was able to get the desired behavior by creating a metaclass.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant