如何将多个实例变量和方法调用到函数中?

时间:2016-12-07 01:23:20

标签: python python-3.x class instance-variables

我正在编写这些类和一个函数来存储该数据库的文件数据。我给了一个文件,文件所有者#和文件号#。我想移动额外的文件 使用匹配的文件所有者和数据库中的数字,并将其放在指定所有者的文件所有者列表中。它返回None但如果所有者已经拥有该文件号为#的文件,则会引发DuplicateIdError。 如果数据库中没有所有者或文件,它将引发MissingIdError。 我的问题是,我如何将多个实例变量和方法从另一个类调用到我的函数中(文件类和所有者类到我目前的数据库类?

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 

2 个答案:

答案 0 :(得分:1)

由于您拥有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

答案 1 :(得分:1)

为了理解如何访问其他类中的变量,实例和方法,您可以阅读此question and answers in stackoverflow

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

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