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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:29:44+00:00 2026-06-12T01:29:44+00:00

Possible Duplicate: Subclassing Python tuple with multiple __init__ arguments I want to define a

  • 0

Possible Duplicate:
Subclassing Python tuple with multiple __init__ arguments

I want to define a class which inherits from tuple, and I want to be able to instantiate it using a syntax not supported by tuple. For a simple example, let’s say I want to define a class MyTuple which inherits from tuple, and which I can instantiate by passing two values, x and y, to create the (my) tuple (x, y). I’ve tried the following code:

class MyTuple(tuple):
    def __init__(self, x, y):
        print("debug message")
        super().__init__((x, y))

But when I tried, for example, MyTuple(2, 3) I got an error: TypeError: tuple() takes at most 1 argument (2 given). It seems my __init__ function was not even called (based on the error I got and on the fact my “debug message” was not printed).

So what’s the right way to do this?

I’m using Python 3.2.

  • 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-12T01:29:46+00:00Added an answer on June 12, 2026 at 1:29 am
    class MyTuple(tuple):
        def __new__(cls, x, y):
            return tuple.__new__(cls, (x, y))
    
    x = MyTuple(2,3)
    print(x)
    # (2, 3)
    

    One of the difficulties of using super is that you do not control which classes’s method of the same name is going to be called next. So all the classes’ methods have to share the same call signature — at least the same number of items. Since you are changing the number of arguments sent to __new__, you can not use super.


    Or as Lattyware suggests, you could define a namedtuple,

    import collections
    MyTuple = collections.namedtuple('MyTuple', 'x y')
    
    p = MyTuple(2,3)
    print(p)
    # MyTuple(x=2, y=3)
    print(p.x)
    # 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Javascript window resize event I want to get an event which is
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Subclassing UIView to draw in Quartz Hello i am searching good example
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?

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.