Python: can a class instance evaluate to any type?
Question: 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?


Answer: 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] )
    

