0

So let's say you have something like this:

Class Person:
    height = """6' 0"""

and

Henry = Person

What do I have to define in the class to do Henry and get '6' 0"' and not something like <class __main__.Person 0x00012040>?

LavaHot
  • 382
  • 2
  • 20

1 Answers1

5

First, you need to instantiate the class.

Henry = Person()

Then, you need to define the __repr__() method.

Class Person:
    def __repr__(self):
        return self.height

And finally, you need to make it an instance attribute, not a class attribute.

Class Person:
    def __init__(self):
        self.height = "6' 0\""
Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • The OP could just do `Person.height` but yes, it is most likely what they're asking for, and nicely explained so +1 from me – Jon Clements Jul 01 '12 at 04:28
  • Oh right, `Henry = Person` just sets Henry **equal** to the Person class, not instantiating it. Thank you! – LavaHot Jul 01 '12 at 04:49