@@ -18,7 +18,7 @@ def __init__(self,name,roll):
18
18
# we can do this if we pass the attributes when creating a student class.
19
19
20
20
def show (self ):
21
- print (f"STUDENT INFORMATION\n name : { self ._name } \n roll : { self ._roll } " )
21
+ print (f"STUDENT INFORMATION \n \t name : { self ._name } \n \t roll : { self ._roll } " )
22
22
23
23
# students also have a laptop, so we want a laptop attribute in this class.
24
24
# but the problem is when we talk about laptop,
@@ -33,7 +33,7 @@ def __init__(self,brand,cpu,ram):
33
33
self .ram = ram
34
34
35
35
def show (self ):
36
- print (f"LAPTOP CONFIGURATION\n brand : { self .brand } \n cpu : { self .cpu } \n ram : { self .ram } " )
36
+ print (f"LAPTOP CONFIGURATION \n \t brand : { self .brand } \n \t cpu : { self .cpu } \n \t ram : { self .ram } " )
37
37
38
38
39
39
# to create the object of the inner laptop class in the outer class, we can do that in the __init__ method.
@@ -44,17 +44,21 @@ def show(self):
44
44
45
45
s1 .show ()
46
46
47
- # creating Laptop object for students
48
- s1_lap = s1 .Laptop ("Lenovo" ,"i5" ,4 )
49
- s2_lap = s2 .Laptop ("HP" ,"i3" ,2 )
47
+ # creating Laptop object for students.
48
+ s1 .lap = s1 .Laptop ("Lenovo" ,"i5" ,4 )
49
+ s2 .lap = s2 .Laptop ("HP" ,"i3" ,2 )
50
+ # this object will be an attribute of the student object.
50
51
51
52
# we can print the attributes of Laptop using,
52
- print (s1_lap .brand )
53
+ print (s1 . lap .brand )
53
54
54
- # we haev two different classes with the same name. but we can access both of them.
55
+ # we have two different classes with the same name. but we can access both of them.
55
56
print ()
56
57
s1 .show ()
57
58
print ()
58
- s1_lap .show ()
59
+ s1 . lap .show ()
59
60
60
61
# NOTE: it is not simmilar to inheritance.
62
+ # it can reduce potential name conflicts because it allows for a simmilarly named class to exists in another context.
63
+ # another advantage is that it allows for a more advanced form of inheritance
64
+ # in which a subclass of the outerclass can override the defination of its inner class.
0 commit comments