当另一个变更时,更改实例变量

时间:2016-01-26 20:57:46

标签: python class

我希望每次在类上进行变量更改时,都能够更改类中的变量(在本例中为greet)。

剪辑就是这个:

class Greet(object):                                                                                        
    def __init__(self, name="", age="", job=""):
        self.name = name
        self.age = age
        self.job = job
        self.greet = "Hello"

        if self.name:
            self.greet = self.greet + " " + self.name 

        if self.age:
            self.greet = self.greet + ". Good to know that you is " + self.age + " years old"

        if self.job:
            self.greet = self.greet + ". I think being a " + self.job + " is amazing."

    def shout(self):
        print self.greet
        return True

g1 = Greet(name="John Doe", job="Programmer")

g2 = Greet(name="Janis Duh", age="34", job="Singer")
g2.name = "Janis Joplin"

g3 = Greet()
g3.name = "Smith"
g3.age = "22"
g3.job = "Designer"

print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g1.name, g1.age, g1.job, g1.greet))
g1.shout

print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g2.name, g2.age, g2.job, g2.greet))
g2.shout

print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g3.name, g3.age, g3.job, g3.greet))
g3.shout

输出是:

  

姓名John Doe
  年龄
  工作   程序员
  迎接你好John Doe。我认为   成为程序员真是太棒了。

     

名字Janis Joplin
  年龄34岁   Job Singer
  迎接你好Janis Duh。很高兴知道你今年34岁。我认为成为一名歌手真是太棒了。

     

姓名史密斯   22岁   工作设计师
  问候你好

在任何情况下,喊叫方法都无法正确打印“问候语”。即使在正确设置self.greet的情况下也是如此。

g1对象打印正常,因为我在创建实例时声明了变量。

g2正确更改变量名称,但问候语仍指向旧值,似乎未更新。

g3更糟糕的是,可能出于同样的原因,g2没有在问候语中正确更新名称。

欢迎指示

(后期编辑)

在亚当·斯密之后,提示我宁愿将“问候”保持为实例属性,如:

def greet(self):
    self.greeting = "Hello!"
    if self.name:
        self.greeting += " {0.name}."
    if self.age:                                                                                        
        self.greeting += " Good to know you are {0.age} years old."
    if self.job:
        self.greeting += " I think being a {0.job} is amazing."
    print(self.greeting.format(self))

...

g = Greeter(...)
g.greet()

工作,但

print g.greeting

给我一​​些类似的东西:

Hello! {0.name}. Good ...

2 个答案:

答案 0 :(得分:3)

听起来你需要在一个函数中生成它,所以每次调用它时都要计算它。

class Greeter(object):
    def init(self, ...):
        ...

    def greet(self):
        greeting = "Hello!"
        if self.name:
            greeting += " {0.name}."
        if self.age:
            greeting += " Good to know you are {0.age} years old."
        if self.job:
            greeting += " I think being a {0.job} is amazing."
        print(greeting.format(self))

答案 1 :(得分:2)

self.greet正在__init__期间计算,并且不会再次重新计算。

我最好的方法是让greet属性。因此,您仍然可以使用您拥有的语法

def __init__(self, name="", age="", job=""):
    self.name = name
    self.age = age
    self.job = job

@property
def greet(self):
    txt = 'Hello'
    if self.name:
        txt += " " + self.name

    if self.age:
        txt += ". Good to know that you is " + self.age + " years old"

    if self.job:
        txt += ". I think being a " + self.job + " is amazing."

    return txt

通过这种方式greet每次get时都会重新计算。

现在执行测试代码:

g1 = Greet(name="John Doe", job="Programmer")

g2 = Greet(name="Janis Duh", age="34", job="Singer")
g2.name = "Janis Joplin"

g3 = Greet()
g3.name = "Smith"
g3.age = "22"
g3.job = "Designer"
print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g1.name, g1.age, g1.job, g1.greet))
g1.shout()

print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g2.name, g2.age, g2.job, g2.greet))
g2.shout()

print ("Name %s\nAge %s\nJob %s\nGreet %s\n" % ( g3.name, g3.age, g3.job, g3.greet))
g3.shout()

生成此输出:

Name John Doe
Age
Job Programmer
Greet Hello John Doe. I think being a Programmer is amazing.

Hello John Doe. I think being a Programmer is amazing.
Name Janis Joplin
Age 34
Job Singer
Greet Hello Janis Joplin. Good to know that you is 34 years old. I think being a Singer is amazing.

Hello Janis Joplin. Good to know that you is 34 years old. I think being a Singer is amazing.
Name Smith
Age 22
Job Designer
Greet Hello Smith. Good to know that you is 22 years old. I think being a Designer is amazing.

Hello Smith. Good to know that you is 22 years old. I think being a Designer is amazing.

在调用greet

时,实例变量的手动打印与shout的手动打印相同