Reputation: 111
I'm writting these classes and a function to store file data for this database. I'm given a File, with a file owner# and a file number#. I want to move the extra file with the matching file owner and number from the Database and put it in the file owners list of the indicated owner. It returns None but it Raises a DuplicateIdError if the owner already has a the file with that file number#. It will raise a MissingIdError if the owner or file doesn't exist in the database. My question is, how do I call multiple instance-variables and methods from another class into my function (File Class and Owners Class to the Database class I'm currently in?
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
Upvotes: 1
Views: 2299
Reputation: 810
Because you have instances of Owner
s and File
s, you'll need an initializer in your classes, aka __init__()
. Your database holds a reference to a list of owners. Then you can iterate through those owners to find the right one.
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
Then, to make use of these, you could do something like such:
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
Upvotes: 1
Reputation: 9257
In order to understand how to access variables, instances and methods in another classes, you can read this question and answers in stackoverflow
However, this is a basic example how to do it:
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()
Output:
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
Upvotes: 1