Python:子类属性不固定

时间:2018-10-16 16:17:35

标签: python-3.x parent-child

我正在尝试使用课程,但对他们来说仍然是新手。我对如何从父类继承属性以及为子类初始化新属性感到困惑。我发现父级属性是可调用的,但我定义的子级属性却不是。请在下面查看我的代码:

import random
import hashlib
import base64
import requests
import json
import urllib3
import getpass

# Disable warning re: server certificate checking turned off
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

class vsa:
    def __init__(self):
        self.vsa_token = self.api_authentication()
        self.headers = {'Authorization': "Bearer " + self.vsa_token}
        self.url = "example.com"

    def api_authentication(self):
        while True: 
            username = input("Enter your username: ")
            password = getpass.getpass("Enter your password: ")

            vsa = "example.com/api/auth"
            random_integer = random.randint(5000, 50000)
            CoveredSHA256HashTemp_object = hashlib.sha256()
            CoveredSHA256HashTemp_object.update(('%s%s' % (password, username)).encode('utf-8'))
            CoveredSHA256HashTemp = CoveredSHA256HashTemp_object.hexdigest()
            CoveredSHA256Hash_object = hashlib.sha256()
            CoveredSHA256Hash_object.update(('%s%d' % (CoveredSHA256HashTemp, random_integer)).encode('utf-8'))
            CoveredSHA256Hash = CoveredSHA256Hash_object.hexdigest()
            CoveredSHA1HashTemp_object = hashlib.sha1()
            CoveredSHA1HashTemp_object.update(('%s%s' % (password, username)).encode('utf-8'))
            CoveredSHA1HashTemp = CoveredSHA1HashTemp_object.hexdigest()
            CoveredSHA1Hash_object = hashlib.sha1()
            CoveredSHA1Hash_object.update(('%s%d' % (CoveredSHA1HashTemp, random_integer)).encode('utf-8'))
            CoveredSHA1Hash = CoveredSHA1Hash_object.hexdigest()
            auth = "user=%s, pass2=%s, pass1=%s, rpass2=%s, rpass1=%s, rand2=%d, twofapass=:undefined" % (username, CoveredSHA256Hash, CoveredSHA1Hash, CoveredSHA1Hash, CoveredSHA256Hash, random_integer)
            encode = base64.urlsafe_b64encode(('%s' % auth).encode('UTF-8')).decode('ascii')
            headers = { 'Authorization' : 'Basic %s' %  encode }
            r = requests.get(vsa, headers=headers, verify=False)

            data = r.text

            parsed_data = json.loads(data)
            if parsed_data['ResponseCode'] == 0:
                print("\nVSA token successfully retrieved\n")
                vsa_token = parsed_data['Result']['Token']
                return vsa_token
            else:
                print("Unauthorized")


    def get_vsa_token(self):
        return self.vsa_token

class agents(vsa):

    def __init___(self):
        vsa.__init__(self)
        self.call = "/asetmgmt/agents"

    def get_agents(self):
        list_agents = []
        r = requests.get(self.url + self.call, headers=self.headers, verify=False)
        data = r.text
        parsed_data = json.loads(data)
        all_agents = parsed_data
        for agents in all_agents['Result']:
                    list_agents.append((agents['AgentName']))
        return list_agents

session = agents()
print(session.call)

最后一个命令-print(session.call)给我以下输出:

AttributeError: 'agents' object has no attribute 'call'

但是,我已经(相信是这样)使用了这段代码的相关部分,并通过PythonTutor运行了它,并看到call属性正在初始化?

PythonTutor Link to Code

我对自己做错的事感到茫然。我发现我可以在agents.__init__(self)块之外初始化变量,并且可以正常工作:

class agents(vsa):

    call = "/assetmgmt/agents"

    def __init___(self):
        vsa.__init__(self)

但现在这是一个概念问题,困扰着我。我在构造这个方法上愚蠢吗?您会如何改善结构?

感谢您提供的任何见解。

1 个答案:

答案 0 :(得分:0)

感谢@Blckknght为我提供了解决方案,该修复程序解决了我所做的错字...

__init___!= __init__

总是简单的事情被忽略。我很高兴在这里发布了一个问题,而不是不断调试和尝试其他解决方案(我大概花了30分钟的时间才这么做)。

谢谢!