在另一个类中创建一个类对象

时间:2020-04-13 16:40:13

标签: python python-3.x

我试图创建一个类对象quantity_sum并在类对象Credentials中执行方法get_username_password

Session

如果我使用class Credentials: def __init__( self, username=None, password=None, client_id=None, client_secret=None): self.username = username self.password = password self.client_id = client_id self.client_secret = client_secret def get_username_password(self): self.username = input("please enter your username: ") self.password = input("please enter your password: ") class Session: def __init__( self): self.credentials = Credentials() self.credentials = self.credentials.get_username_password() if __name__ == "__main__": a = Credentials() a.get_username_password() b = Session() 进行操作,则bb.credentials类型的对象。

4 个答案:

答案 0 :(得分:0)

我认为您正在寻找:

class Credentials:
    def __init__(
            self,
            username=None,
            password=None,
            client_id=None,
            client_secret=None):

        self.username = username
        self.password = password
        self.client_id = client_id
        self.client_secret = client_secret

    def get_username_password(self):
        self.username = input("please enter your username: ")
        self.password = input("please enter your password: ")

class Session:
    def __init__(
            self):
        self.credentials = Credentials()

    def get_username_password(self):
        self.credentials.get_username_password()

答案 1 :(得分:0)

get_username_password作为一种提示用户输入数据然后将结果作为必需的参数传递给__init__的类方法更有意义。

class Credentials:
    def __init__(
            self,
            username,
            password,
            client_id=None,
            client_secret=None):

        self.username = username
        self.password = password
        self.client_id = client_id
        self.client_secret = client_secret

    @classmethod
    def from_user_input(cls):
        username = input("please enter your username: ")
        password = input("please enter your password: ")
        return cls(username, password)

是否应该成为Credentials的成员还是有待商bat的,因为您也不应该从Session.__init__来打电话。

class Session:
    def __init__(self, c):
        self.credentials = c

    @classmethod
    def from_user_input(cls):
        c = Credentials.from_user_input()
        return cls(c)

if __name__ == "__main__":
    a = Credentials.from_user_input()
    b = Session(a)
    # b = Session.from_user_input()

作为一般规则,请尽可能使I / O脱离__init__方法。

答案 2 :(得分:0)

您好,Steffen。希望您一切都好。

问题的症结在于您要实例化两个单独的 Credentials 对象。实例化 a 作为 Credentials 对象,然后实例化 SEPARATE Credentials 对象。 Session 对象。

我知道有一些解决此问题的方法。



1。将凭据对象作为初始化参数传递给Session对象。


class Credentials:
    def __init__(
            self,
            username=None,
            password=None,
            client_id=None,
            client_secret=None):

        self.username = username
        self.password = password
        self.client_id = client_id
        self.client_secret = client_secret

    def get_username_password(self):
        if not self.username:
            self.username = input("please enter your username: ")
        if not self.password:
            self.password = input("please enter your password: ")

class Session:
    def __init__(
            self,
            credentials=None):
        if credentials:
            self.credentials = credentials
        else:
            self.credentials = Credentials()

        if not (credentials.username and credentials.password):
            self.credentials.get_username_password()

if __name__ == "__main__":
    a = Credentials()
    b = Session(credentials=a)

2。用** Credentials 创建一个单例类。这意味着您只有一个对象代表整个类。换句话说,无论您在何处实例化它,都可以访问 Credentials 属性的相同 实例。如果您想进一步调查,请使用tutorial。我认为这不是您要实现的目标。**


3。可能还有其他选择。我并不精通所有方法。



我希望这会有所帮助!祝你好运!

答案 3 :(得分:0)

post-compile weave mode 这里的问题是self.credentials = self.credentials.get_username_password()是一个对象,并且您正在对此对象调用self.credentials方法,但是此方法不返回任何内容,因此没有任何内容进入get_username_password,因此它变成了{{ 1}}类型的对象。

您可以尝试这样的事情。

b.credentials