在python中调用同一类的另一个方法中方法的变量值

时间:2017-10-16 07:39:32

标签: python-3.x

我想在最后一个函数(inquiry())中访问父亲的名字,并且该名称出现在father()函数中。如何在father()函数中访问name变量的值?

class family(object):
    def __init__(self,members,surname):
        self.members=members
        self.surname=surname
    def father(self,name,occupation):
        self.name=name
        self.occupation=occupation
    def mother(self,name,occupation):
        self.name=name
        self.occupation=occupation
    def children(self,numbers):
        self.numbers=numbers
    def enquiry(self):
        print("The name of the father is "self.father(name))

family(4,'vora')
family.father('john','business')
family.enquiry()

1 个答案:

答案 0 :(得分:1)

这段代码纠正了你的句法错误,但你肯定应该接受成员P i的建议,因为这很丑陋

class family(object):
    def __init__(self,members,surname):
        self.members=members
        self.surname=surname
    def father(self,name,occupation):
        self.fathers_name=name
        self.occupation=occupation
    def mother(self,name,occupation):
        self.mothers_name=name
        self.occupation=occupation
    def children(self,numbers):
        self.numbers=numbers
    def enquiry(self):
        print("The name of the father is " + self.fathers_name)

f = family(4,'vora')
f.father('john','business')
f.enquiry()