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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:34:01+00:00 2026-06-03T08:34:01+00:00

A class can act as a string through its __str__ method, or as a

  • 0

A class can act as a string through its __str__ method, or as a function via its __call__ method. Can it act as, say, a list, or a tuple?

class A (object):
    def __???__ (self):
        return (1, 2, 3)

>>> a = A()
>>> a * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)

EDIT…

Here’s a better example to help clarify the above.

class Vector (object):
    def __init__ (self):
        self.vec = (1,2,3)
    def __???__ (self):
        # something like __repr__; non-string
        return self.vec

class Widget (object):
    def __init__ (self):
        self.vector = Vector()

>>> w = Widget()
>>> w.vector
(1, 2, 3) # not a string representation (at least, before being repr'd here)

Basically, I want something like __repr__ that doesn’t return a string, but returns a tuple (or list) when I simply invoke the name pointing to the Vector instance, but I don’t want to lose the rest of the abilities in the instance, like access to other properties and methods. I also don’t want to have to use w.vector.vec to get to the data. I want vector to act like a tuple attribute of w, while still being able to do something like w.vector.whatever(), or overriding __mul__ so I can scale the vector via w.vector * 5. Possible?

  • 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-03T08:34:02+00:00Added an answer on June 3, 2026 at 8:34 am

    Depending on what your goal is, you can create a class that inherits from built-in classes like list or tuple:

    >>> class A(tuple):
    ...     def speak(self):
    ...         print "Bark!"
    ... 
    >>> a = A((1,2,3)) # extra parens needed to distinguish single tuple arg from 3 scalar args
    >>> a * 3
    (1, 2, 3, 1, 2, 3, 1, 2, 3)
    >>> a.speak()
    Bark!
    

    Given your Vector use case, subclassing tuple might well do the trick.

    import math
    
    class Vector(tuple):
        def magnitude(self):
            return math.sqrt( self[0]*self[0]+self[1]*self[1]+self[2]*self[2] )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In actionscript object class can act as a collection that stores key,value combinations: var
A class can be a subclass of itself if its inner class extend the
What Swing class can I use to implement something like this? Add To List
Adding functionality to a class can be done by adding a method or by
Given an instantiated JavaScript object/class can I make an Ajax call from that class
How can I make a string act as a code? For instance, I want
Having such class can I instatiate it in place? abstract class Mammal { public
In general reflection in a base class can be put to some good and
If a public constructor in an abstract class can only be called by their
I am trying to access child class variables in parent class..can u suggest me

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.