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 Duplicates: How can I convert ereg expressions to preg in PHP? What is
Possible Duplicate: Why can't I unbox an int as a decimal? Okay, C#/.NET gurus,
I have some expensive function f(x) that I want to only calculate once, but
Possible Duplicate: Why would a javascript variable start with a dollar sign? I see
Possible Duplicate: C: Where is union practically used? I know the concept of union
Possible Duplicate: comparing contents of two files using python I have a file name
Possible Duplicate: Difference between .Net4 Client Profile and Full Framework download I am writing
Possible Duplicate: C# Lambda ( => ) For instance Messenger.Default.Register<AboutToCloseMessage>(this, (msg) => { if
Possible Duplicate: Howto build a SQL statement with using IDs that might not be
Possible Duplicate: Using a javascript variable to set PHP variable I need to do

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.