0

Hello I have declared a class in Python and then I want to make a list of objects of this class and print it. I am new to python and I cannot figure out what I am doing wrong. I know C++ and this is what I would like to do

class Word:

    def __init__(self,word,hor):
        self.word=word
        self.x1=0
        self.y1=0
        self.x2=0
        self.y2=0
        self.hor=hor

    def get_dimensions(self):
        return(self.x1,self.y1,self.x2,self.y2)

    def set_dimensions(self,t):
        self.x1=t[0]
        self.y1=t[1]
        self.x2=t[2]
        self.y2=t[3]

    def get_horizontal():
        return self.hor

    def display(self):
        print word

def WordList(word_list,hor):
    l=[]
    for x in word_list:
        w1=Word(x,hor)
        l.append(w1)
    return l


li=["123","23","43"]
li=WordList(li,True)
for x in li:
    x.display #obviously something else has to be done here

Also I get the following compilation problem when I try to run it:

[<__main__.Word instance at 0x7ffc9320aa70>, <__main__.Word instance at 0x7ffc9320ab00>, <__main__.Word instance at 0x7ffc9320ab48>]

Can you help me?

JmRag
  • 1,443
  • 7
  • 19
  • 56
  • 1
    what's the problem here? Everything looks good – charlee Feb 27 '14 at 17:30
  • That's not a compilation problem. That's the printed output of a list of objects. – David Robinson Feb 27 '14 at 17:31
  • @DavidRobinson I didn't knew it. Thx for the info! – JmRag Feb 27 '14 at 17:33
  • 1
    As a side note, it's generally considered a good practice to name your classes using `CamelCase` and your functions/methods using the `lowercase_with_underscores` style...so that people like me don't get confused when they glance over your code and assume `WordList` is a class. Check out [PEP8](http://legacy.python.org/dev/peps/pep-0008/) for more information. – Tyler MacDonell Feb 27 '14 at 17:35
  • 1
    having a display function like that isn't very pythonic, implement `__str__` function on the object then use `print x` instead of `x.display()` – cmd Feb 27 '14 at 17:37
  • @cmd I know this was just a test function for debbuging purposes. Thx for the advice though! – JmRag Feb 27 '14 at 17:39

2 Answers2

2

You need to fix two bugs:

def display(self):
    print self.word #Added self here

and

for x in li:
    x.display() #Added brackets here
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • Thx now it works. But here is my question: Are the instances of the words in the list different or not? – JmRag Feb 27 '14 at 17:34
  • @JmRag Sorry, I'm not quite sure how to understand your question. Do you mean `li=["123","23","43"]` and `li=WordList(li,True)`? – Uli Köhler Feb 27 '14 at 17:35
  • @JmRag They're different because a new one is initialized with each `w1=Word(x,hor)`. You can confirm they're different just from the pointer addresses in the printed output: `0x7ffc9320aa70` vs `0x7ffc9320ab00`, for instance. – David Robinson Feb 27 '14 at 17:36
  • UliKöhler I meant if the object of the list are different from one another and like @David Robisnon answered , they are – JmRag Feb 27 '14 at 17:38
  • @JmRag I've updated my answer to explain the comparisons in more detail. – SleepyCal Feb 27 '14 at 17:38
2

You are attempting to print the method itself, rather than call it.

Use the following instead:

for x in li:
    x.display()

You can also provide a custom str method;

class SomeClassHere(object):
    def __init__(self, a):
        self.a = a
    def __str__(self):
        return "Hello %s" % ( self.a, )

>>> a = SomeClassHere(a="world")
>>> print a
Hello world

To answer your additional question on whether the types match or not;

>>> class Hello(object):
...     def __init__(self, a):
...         self.a = a
... 
>>> b = Hello(a=1)
>>> c = Hello(a=2)
>>> d = Hello(a=3)
>>> b == c
False
>>> c == d
False
>>> isinstance(b, Hello)
True

You can change this behaviour by modifying __eq__ and __cmp__ - see:

How is __eq__ handled in Python and in what order?

Community
  • 1
  • 1
SleepyCal
  • 5,739
  • 5
  • 33
  • 47