Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python3 super未初始化初始化属性_Python 3.x_Inheritance_Super - Fatal编程技术网

Python 3.x Python3 super未初始化初始化属性

Python 3.x Python3 super未初始化初始化属性,python-3.x,inheritance,super,Python 3.x,Inheritance,Super,我有以下代码片段: class BaseUserAccount(object): def __init__(self): accountRefNo = "RefHDFC001" FIType = "DEPOSIT" pan = "AFF34964FFF" mobile = "9822289017" email = "manoja@cookiejar.co.in" aadhar = "55555

我有以下代码片段:

class BaseUserAccount(object):
    def __init__(self):
        accountRefNo = "RefHDFC001"
        FIType = "DEPOSIT"
        pan = "AFF34964FFF"
        mobile = "9822289017"
        email = "manoja@cookiejar.co.in"
        aadhar = "5555530495555"


class TestUserSavingsAccount(BaseUserAccount):
    def __init__(self):
        super().__init__()
        accountNo = "HDFC111111111111"
        accountTypeEnum = "SAVINGS"

    def test_create_account(self):
        request_body = """\
            <UserAccountInfo>
                <UserAccount accountRefNo="{}" accountNo="{}"
                accountTypeEnum="{}" FIType="{}">
                    <Identifiers pan="{}" mobile="{}" email="{}" aadhar="{}"></Identifiers>
                </UserAccount>
            </UserAccountInfo>
        """.format(self.accountRefNo, self.accountNo, self.accountTypeEnum,
                self.FIType, self.pan, self.mobile, self.email, self.aadhar)

看到上面的行为,似乎
super
既没有设置基类中的值,也没有设置子类(
accountNo
accountTypeEnum
)的属性。您需要初始化
self
对象的属性:

class BaseUserAccount(object):
    def __init__(self):
        self.accountRefNo = "RefHDFC001"
        self.FIType = "DEPOSIT"
        self.pan = "AFF34964FFF"
        self.mobile = "9822289017"
        self.email = "manoja@cookiejar.co.in"
        self.aadhar = "5555530495555"


class TestUserSavingsAccount(BaseUserAccount):
    def __init__(self):
        super().__init__()
        self.accountNo = "HDFC111111111111"
        self.accountTypeEnum = "SAVINGS"

您编写的方法仅将这些值分配给局部变量。您需要初始化
self
对象的属性:

class BaseUserAccount(object):
    def __init__(self):
        self.accountRefNo = "RefHDFC001"
        self.FIType = "DEPOSIT"
        self.pan = "AFF34964FFF"
        self.mobile = "9822289017"
        self.email = "manoja@cookiejar.co.in"
        self.aadhar = "5555530495555"


class TestUserSavingsAccount(BaseUserAccount):
    def __init__(self):
        super().__init__()
        self.accountNo = "HDFC111111111111"
        self.accountTypeEnum = "SAVINGS"