设置一个属性等于另一个

时间:2013-01-29 23:48:11

标签: python class inheritance attributes

抱歉新手问题,但我对编程很新。我的问题是,当从父类继承更多属性而不是我需要时,如何设置其中一个属性等于另一个属性?下面是一个例子:

class numbers():
     def __init__(self, x, y, z):
     # x, y and z initialized here

class new_numbers(numbers):
     def __init__(self, x, y):
        numbers.__init__(self, x, y=x, z)
        # what im trying to do is get the y attribute in the subclass to be equal to the x attribute, so that when the user is prompted, they enter the x and z values only.

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

你需要这样的东西:

class numbers(object):
    def __init__(self,x,y,z):
        # numbers initialisation code

class new_numbers(numbers):
    def __init__(self,x,z):
        super(new_numbers,self).__init__(x,x,z)
        # new_numbers initialisation code (if any)

答案 1 :(得分:0)

你的意思是这样吗?

class numbers():
     def __init__(self, x, y, z):
         # x, y and z initialized here

class new_numbers(numbers):
     def __init__(self, x, z):
        numbers.__init__(self, x, x, z)

在函数/方法调用中的关键字之后,不能使用非关键字参数。