0
lis.py

class mylist:
        def __init__(self,l):
                self.data = l;
        def __add__(self,l):
                self.data = self.data + l
        def __repr__(self):
                return self.data

main.py

from lis import*

x = mylist([1,2,3])
x = x + [4,5]
print x

Why is it printing None? What do I did wrong,Thanks.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by vbx_wx
0

Some print statements should clear up some things. And note that your __add__ function will yield an error but that is something to address after you get this part working.

class mylist:
        def __init__(self,l):
                self.data = l;
        def __add__(self,l):
                self.data = self.data + l
        def __repr__(self):
                return self.data
 
x = mylist([1,2,3])
print x
print x.data

#x = x + [4,5]
#print x

Edited by woooee: n/a

0

i guess my problem was that i didnt return from __repr__ with `self.data` and in main.py I shouldnt done directly x + [4,5].

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.