如何调用一个函数的多个实例变量和方法?

How to call multiple instance-variables and methods to a function?

我正在编写这些 classes 和一个函数来为这个数据库存储文件数据。我得到一个文件,文件所有者# 和文件编号#。我想移动额外的文件 使用数据库中匹配的文件所有者和编号,并将其放入指定所有者的文件所有者列表中。它 returns None 但如果所有者已经拥有具有该文件编号# 的文件,它会引发 DuplicateIdError。 如果数据库中不存在所有者或文件,它将引发 MissingIdError。 我的问题是,如何将另一个 class 的多个实例变量和方法调用到我的函数中(文件 Class 和所有者 Class 到数据库 class 我现在在?

class File:         
    self.file_num
class Owners:
    self.owner_id        
    self.owner_list
class Database:
    def loan_book(self, owner_id, file_num):
        extra_file = file # Calls file from File Class?
        for i in self.owner_id: # Calls from Owners Class?
            for j in self.owner_list: # Calls from Owners Class?
                if extra_file == owner_id and file_num:
                    raise DuplicateIdError
                elif extra_file != owner_id and file_num:
                    extra_file.append(self.owner_list)
                else:           
                    raise MissingIdError 

因为您有 OwnerFile 的实例,所以您需要在 类 中有一个初始化程序,也就是 __init__()。您的数据库包含对所有者列表的引用。然后您可以遍历这些所有者以找到合适的所有者。

class File:
    def __init__(self, num):         
        self.num = num

class Owner:
    def __init__(self, id, files=[]):
        self.id = id
        self.files = files 

    def add_file(self, file_num):
        extraFile = File(file_num)
        self.files.append(extraFile)

class Database:
    def __init__(self, owners=[]):
        self.owners = owners

    def loan_book(self, owner_id, file_num):
        for owner in owners:
            if owner.id == owner_id:
                for file in owner.files:
                    if file.id == file_num:
                        raise DuplicateIdError
                owner.add_file(file_num)
        raise MissingIdError

然后,要利用这些,您可以这样做:

f = File(1)
owner1 = Owner(1, [f])
owner2 = Owner(2)

db = Database([owner1, owner2])
db.loan_book(2, 1) # Adds file with id 1 to owner2's list
db.loan_book(1, 1) # Raises DuplicateIdError
db.loan_book(3, 1) # Raises MissingIdError

为了了解如何访问另一个类中的变量、实例和方法,您可以阅读这篇question and answers in Whosebug

但是,这是一个基本的示例:

class A:
    VAR_A = "i'm var_a"
    def __init__(self):
        self.a = "I'm in class A"

    def method_a(self):
        return "i'm method_a"

class B:
    VAR_B = "i'm var_b"
    def __init__(self):
        self.b = "I'm in class B"

    def method_b(self):
        return "i'm method_b"

class C:
    def __init__(self):
        # Initialise class/object A
        A.__init__(self)
        # create an instance variable which hold A() object
        self.class_a = A()
        # Initialiser class/object B
        B.__init__(self)
        # create an instance variable which hold B() object
        self.class_b = B()

    def my_function(self):
        # calling a which is an instance variable of the class A
        print("Expected output: I'm in class A\t\tGot: ", self.class_a.a)
        # calling VAR_A which is A's class attribute
        print("Expected output: i'm var_a\t\tGot: ", self.class_a.VAR_A)
        # calling method_a which is an A's method
        print("Expected output: i'm method_a\t\tGot: ", self.class_a.method_a())

        # calling b which is an instance variable of the class B        
        print("Expected output: I'm in class B\t\tGot: ", self.class_b.b)
        # calling VAR_B which is B's class attribute
        print("Expected output: i'm var_b\t\tGot: ", self.class_b.VAR_B)
        # calling method_b which is an B's method
        print("Expected output: i'm method_b\t\tGot: ", self.class_b.method_b())

# Run the script
if __name__ == '__main__':
    app = C()
    app.my_function()

输出:

Expected output: I'm in class A     Got:  I'm in class A
Expected output: i'm var_a          Got:  i'm var_a
Expected output: i'm method_a       Got:  i'm method_a
Expected output: I'm in class B     Got:  I'm in class B
Expected output: i'm var_b          Got:  i'm var_b
Expected output: i'm method_b       Got:  i'm method_b