Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:18:58+00:00 2026-06-11T10:18:58+00:00

Suppose following class: class Class(object): @classmethod def getitem(*args): print ‘getitem %s’ % (args,) @classmethod

  • 0

Suppose following class:

class Class(object):
    @classmethod
    def getitem(*args):
        print 'getitem %s' % (args,)
    @classmethod
    def __getitem__(*args):
        print '__getitem__ %s' % (args,)

The getitem method behaves as expected: it receives Class as first arg, but __getitem__ receives type as first arg:

calling Class.getitem(test)
getitem (<class '__main__.Class'>, 'test')

calling obj.getitem(test)
getitem (<class '__main__.Class'>, 'test')

calling Class[test]
'type' object has no attribute '__getitem__'

calling obj[test]
__getitem__ (<class '__main__.Class'>, 'test')

What is the magic there behind __getitem__ ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:18:59+00:00Added an answer on June 11, 2026 at 10:18 am

    Special methods are looked up on the class, and not on the instance – unlike regular methods that are looked up on the instance first. See Special method lookup in the Python data model docs.

    Thinking about Class as an instance of type, this means when you do

    Class.getitem(test)
    

    It looks first for exactly what you tell it: a method in Class‘s own attributes called getitem. But, when you use

    Class[test]
    

    it skips this, and goes straight to type (being the class of Class, or its metaclass), and so calls type.__getitem__(Class, test). So, what’s happening isn’t that __getitem__ gets type as its first argument (it would still get Class, as it does if you explicitly Class.__getitem__(test)), its that the __getitem__ that Python looks for in this case doesn’t exist. To make it exist, you need to define your own metaclass for Class that defines it as an instance method, rather than defining it on Class as a classmethod.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have following code defined in tester.py class Tester( object ): def method(
Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name
Suppose I had the following method in an object: public class foo { public
Suppose that I have the following python base class: class BaseClass(object): def a(): This
Suppose the following object structure: class Super {} class SubA extends Super {} class
Suppose I have the following service object public class UserService { @Autowired private UserDao
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
Let's suppose I want to create a javascript class/object/function which have a method that
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose the following class public class Message { // some code } And a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.