调试测试代码

时间:2011-05-30 02:15:30

标签: python debugging

我写这篇文章只是为了测试“Property”功能。但是,我在get_name函数中遇到了一个错误。我很感激任何帮助。

#The Property Critter
#Using New Classes vs Old Classes:

class Critter (object):
    def __init__ (self, name):
        print "A new critter has been born."
        self.__name = name

    def get_name (self):
        return self.__name

    def set_name (self, new_name):
        if new_name == "":
            print "The name can't be blank. Ass."
        else:
            self.__name = new_name
            print "Namt change successful."

name = property (get_name, set_name)

crit = Critter ("Pooch Dog")
print crit.name
crit.name = "Mad Dog"
print crit.name

1 个答案:

答案 0 :(得分:3)

您需要缩进name = property (get_name, set_name)以使其成为您的Critter类的一部分:

class Critter (object):
    def __init__ (self, name):
        print "A new critter has been born."
        self.__name = name

    def get_name (self):
        return self.__name

    def set_name (self, new_name):
        if new_name == "":
            print "The name can't be blank. Ass."
        else:
            self.__name = new_name
            print "Namt change successful."

    name = property (get_name, set_name)

crit = Critter ("Pooch Dog")
print crit.name
crit.name = "Mad Dog"
print crit.name