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.

Recommended Answers

All 2 Replies

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

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].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.