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?
Depending on what your goal is, you can create a class that inherits from built-in classes like
listortuple:Given your Vector use case, subclassing tuple might well do the trick.