Creating and accessing object attributes : Str : Class PYTHON TUTORIALS


PYTHON TUTORIALS » Class » Str »

 

Creating and accessing object attributes


class MyClass(object):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        rep = "MyClass objectn"
        rep += "name: " + self.name + "n"
        return rep

    def __cmp__(self, other):
        if self.name > other.name:
            return 1
        if self.name < other.name:
            return -1
        if self.name == other.name:
            return 0      

    def talk(self):
        print "Hi.  I'm", self.name, "n"

crit1 = MyClass("A")
crit1.talk()

crit2 = MyClass("B")
crit2.talk()

print crit1

print crit1.name



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Class
» Str