Python class returning value
If what you want is a way to turn your class into kind of a list without subclassing list
, then just make a method that returns a list:
def MyClass():
def __init__(self):
self.value1 = 1
self.value2 = 2
def get_list(self):
return [self.value1, self.value2...]
>>>print MyClass().get_list()
[1, 2...]
If you meant that print MyClass()
will print a list, just override __repr__
:
class MyClass():
def __init__(self):
self.value1 = 1
self.value2 = 2
def __repr__(self):
return repr([self.value1, self.value2])
EDIT:
I see you meant how to make objects compare. For that, you override the __cmp__
method.
class MyClass():
def __cmp__(self, other):
return cmp(self.get_list(), other.get_list())
Use __new__
to return value from a class.
As others suggest __repr__
,__str__
or even __init__
(somehow) CAN give you what you want, But __new__
will be a semantically better solution for your purpose since you want the actual object to be returned and not just the string representation of it.
Read this answer for more insights into __str__
and __repr__
https://stackoverflow.com/a/19331543/4985585
class MyClass():
def __new__(cls):
return list() #or anything you want
>>> MyClass()
[] #Returns a true list not a repr or string
class MyClass():
def __init__(self, a, b):
self.value1 = a
self.value2 = b
def __call__(self):
return [self.value1, self.value2]
Testing:
>>> x = MyClass('foo','bar')
>>> x()
['foo', 'bar']
I think you are very confused about what is occurring.
In Python, everything is an object:
[]
(a list) is an object'abcde'
(a string) is an object1
(an integer) is an objectMyClass()
(an instance) is an objectMyClass
(a class) is also an objectlist
(a type--much like a class) is also an object
They are all "values" in the sense that they are a thing and not a name which refers to a thing. (Variables are names which refer to values.) A value is not something different from an object in Python.
When you call a class object (like MyClass()
or list()
), it returns an instance of that class. (list
is really a type and not a class, but I am simplifying a bit here.)
When you print an object (i.e. get a string representation of an object), that object's __str__
or __repr__
magic method is called and the returned value printed.
For example:
>>> class MyClass(object):
... def __str__(self):
... return "MyClass([])"
... def __repr__(self):
... return "I am an instance of MyClass at address "+hex(id(self))
...
>>> m = MyClass()
>>> print m
MyClass([])
>>> m
I am an instance of MyClass at address 0x108ed5a10
>>>
So what you are asking for, "I need that MyClass return a list, like list(), not the instance info," does not make any sense. list()
returns a list instance. MyClass()
returns a MyClass instance. If you want a list instance, just get a list instance. If the issue instead is what do these objects look like when you print
them or look at them in the console, then create a __str__
and __repr__
method which represents them as you want them to be represented.
Update for new question about equality
Once again, __str__
and __repr__
are only for printing, and do not affect the object in any other way. Just because two objects have the same __repr__
value does not mean they are equal!
MyClass() != MyClass()
because your class does not define how these would be equal, so it falls back to the default behavior (of the object
type), which is that objects are only equal to themselves:
>>> m = MyClass()
>>> m1 = m
>>> m2 = m
>>> m1 == m2
True
>>> m3 = MyClass()
>>> m1 == m3
False
If you want to change this, use one of the comparison magic methods
For example, you can have an object that is equal to everything:
>>> class MyClass(object):
... def __eq__(self, other):
... return True
...
>>> m1 = MyClass()
>>> m2 = MyClass()
>>> m1 == m2
True
>>> m1 == m1
True
>>> m1 == 1
True
>>> m1 == None
True
>>> m1 == []
True
I think you should do two things:
- Take a look at this guide to magic method use in Python.
Justify why you are not subclassing
list
if what you want is very list-like. If subclassing is not appropriate, you can delegate to a wrapped list instance instead:class MyClass(object): def __init__(self): self._list = [] def __getattr__(self, name): return getattr(self._list, name) # __repr__ and __str__ methods are automatically created # for every class, so if we want to delegate these we must # do so explicitly def __repr__(self): return "MyClass(%s)" % repr(self._list) def __str__(self): return "MyClass(%s)" % str(self._list)
This will now act like a list without being a list (i.e., without subclassing
list
).>>> c = MyClass() >>> c.append(1) >>> c MyClass([1])