Skip to content

Commit 232dcf9

Browse files
5/22/20
1 parent 47290d3 commit 232dcf9

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

oop12 (Inner class).py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self,name,roll):
1818
# we can do this if we pass the attributes when creating a student class.
1919

2020
def show(self):
21-
print(f"STUDENT INFORMATION\nname : {self._name}\nroll : {self._roll}")
21+
print(f"STUDENT INFORMATION \n\t name : {self._name} \n\t roll : {self._roll}")
2222

2323
# students also have a laptop, so we want a laptop attribute in this class.
2424
# but the problem is when we talk about laptop,
@@ -33,7 +33,7 @@ def __init__(self,brand,cpu,ram):
3333
self.ram=ram
3434

3535
def show(self):
36-
print(f"LAPTOP CONFIGURATION\nbrand : {self.brand}\ncpu : {self.cpu}\nram : {self.ram}")
36+
print(f"LAPTOP CONFIGURATION \n\t brand : {self.brand} \n\t cpu : {self.cpu} \n\t ram : {self.ram}")
3737

3838

3939
# 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):
4444

4545
s1.show()
4646

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.
5051

5152
# we can print the attributes of Laptop using,
52-
print(s1_lap.brand)
53+
print(s1.lap.brand)
5354

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.
5556
print()
5657
s1.show()
5758
print()
58-
s1_lap.show()
59+
s1.lap.show()
5960

6061
# 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

Comments
 (0)