CAUsers dylan\'Documentsipsixpy Notepad Edit Search View Encoding Language Setti
ID: 3576360 • Letter: C
Question
CAUsers dylan'Documentsipsixpy Notepad Edit Search View Encoding Language Settings Macro Run plugins Window? class Book def init (self author, title, book id self uthor uthor elf, title title self book id book id def (self) return 'Book s", "la", id) (self author self-title self book id) (self) def repr return Book s", "Ss", sd) self author self title self book id) def (self, other) return self uthor other author and self.title other .title and self.book ide other book id class Patron def init (self, name, patron id, borroweds None self.name name self-patron id patron id elf borrowed m def str (self return Patron ("Sa", "la", Book s", "ss", d) l) 8 (self, name elf patron id, self author se l f title self book i def repr self) return Patron ("ts", s", IBook ("sa", "sa", td) I) (self name self patron id self author elf title sel f book iExplanation / Answer
How to use attributes of one class in another class ? :I would be explaining this with the help of an example.
Example : Assume V1 and V2 are two instance variables ! That means that you have to send the instance of Class1 to Class2 in order for Class2 to access it,
Class1 is being used to initialize both the variables (V1 and V2).
class Class1(object):
def __init__(self):
self.V1 = 1
self.V2 = 2
def method1(self):
self.V1 = self.V1 + self.V2
return self.V1
class Class2(Class1):
def __init__(self, class_a):
self.V1 = class_1.V1
self.V2 = class_1.V2
object1 = Class1()
sum = object1.method2()
object2 = Class2(object1)
print sum
Note : You can access V1 and V2 without sending object1 as a parameter to Class2.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.