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 5985855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:35:23+00:00 2026-05-22T22:35:23+00:00

Is it possible to redefine which object the brackets [] use? I can subclass

  • 0

Is it possible to redefine which object the brackets [] use?

I can subclass the list object, but how to I make the interpreter use my subclass in place of the buildin list object? Is it possible?

(I’m pretty sure I’m using the wrong terms for the question- feel free to edit)

>>> class mlist(list):
...     def __init__(self):
...         list.__init__(self)
...     def __getitem__(self, item):
...         return list.__getitem__(self, item) * 2
... 
>>> testlist = mlist()
>>> testlist.append(21)
>>> testlist[0]
42
>>> list = mlist() # maybe setting the 'list' type will do it?
>>> testlist = []
>>> testlist.append(21)
>>> testlist[0]
21                 # Nope
>>> 

I don’t have a practical use for this- just curious.

  • 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-05-22T22:35:24+00:00Added an answer on May 22, 2026 at 10:35 pm

    Try running the code after you’ve run the code you posted

    >>> testlist = list()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'mlist' object is not callable
    

    Now, determine the type using the code I’ve posted

    >>> type([])
    <type 'list'>
    >>> type(list)
    <class '__main__.mlist'>
    >>> type(testlist)
    <type 'list'>
    

    it seems that [] creates list, instead of mlist, it looks strange :S

    Update

    I checked the bytecode generated using dis, and the code below was generated

    >>> import dis # python's disassembler
    
    >>> def code1():
    ...     return []
    ...
    >>> dis.dis(code1)
      2           0 BUILD_LIST               0
                  3 RETURN_VALUE
    
    >>> def code2():
    ...     return list()
    ...
    >>> dis.dis(code2)
      2           0 LOAD_GLOBAL              0 (list)
                  3 CALL_FUNCTION            0
                  6 RETURN_VALUE
    

    It appears that list will invoke whatever is assigned to it, while [] will be converted to BUILD_LIST bytecode. It appears that [] is not translated to list, hence []‘s behavior is stucked to creating list.

    Update 2

    Python class can be updated

    >>> class NewList(list):
    ...     pass
    ...
    >>> a = NewList()
    >>> a.append(23)
    >>> a[0]
    23
    >>> def double_getitem(self, key):
    ...     return list.__getitem__(self, key) * 2
    ...
    >>> NewList.__getitem__ = double_getitem
    >>> a[0]
    46
    

    Well, except for builtin classes, like list

    >>> list.__getitem__ = double_getitem
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can't set attributes of built-in/extension type 'list'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Redefine Built in PHP Functions Howdy! As my servers do not have
Is it possible to redefine class constants (in PHP)? e.g. class B { const
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Find out the instance id from within an ec2 machine I am
Possible Duplicate: How to store an IP in mySQL I want to get the
In a current project, I have a model called Box that stores items from
In the book of The C++ Programming Language, the author gives the following example
I'm writing C code for a system where address 0x0000 is valid and contains
First of all, I apologize for yet another interface question. I thought that this
I'd like to be able to write something like this in python: 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.