Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 082d91c

Browse files
Merge pull request #18 from yash1rj/master
OOPS Concepts
2 parents 4563e05 + 2a64379 commit 082d91c

File tree

8 files changed

+188
-0
lines changed

8 files changed

+188
-0
lines changed

‎docs/OOPS/Accessing_pvt_var1.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
When we put a double underscore in front of the attribute name, python will internally change its name to _Classname__attribute.
3+
4+
Since we know that the name of the variable changes when we make it private, we can access it using its modified name as shown below:
5+
'''
6+
class Customer:
7+
def __init__(self, cust_id, name, age, wallet_balance):
8+
self.cust_id = cust_id
9+
self.name = name
10+
self.age = age
11+
self.__wallet_balance = wallet_balance
12+
13+
def update_balance(self, amount):
14+
if amount < 1000 and amount > 0:
15+
self.__wallet_balance += amount
16+
17+
def show_balance(self):
18+
print ("The balance is ",self.__wallet_balance)
19+
20+
c1=Customer(100, "Gopal", 24, 1000)
21+
c1._Customer__wallet_balance = 10000000000
22+
c1.show_balance()

‎docs/OOPS/Accessing_pvt_var2.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
To have a error free way of accessing and updating private variables, we create specific methods for this.
3+
Those methods which are meant to set a value to a private variable are called setter methods and methods
4+
meant to access private variable values are called getter methods.
5+
6+
The below code is an example of getter and setter methods:
7+
'''
8+
9+
class Customer:
10+
def __init__(self, id, name, age, wallet_balance):
11+
self.id = id
12+
self.name = name
13+
self.age = age
14+
self.__wallet_balance = wallet_balance
15+
16+
def set_wallet_balance(self, amount):
17+
if amount < 1000 and amount> 0:
18+
self.__wallet_balance = amount
19+
20+
def get_wallet_balance(self):
21+
return self.__wallet_balance
22+
23+
c1=Customer(100, "Gopal", 24, 1000)
24+
c1.set_wallet_balance(120)
25+
print(c1.get_wallet_balance())

‎docs/OOPS/Behavior_in_class.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
We can access an attribute in a method by using self.
3+
Value of the attribute accessed inside the method is determined by the object used to invoke the method.
4+
For example, in the code below when we invoke purchase using mob1, attribute values (Apple and 20000) of mob1 are accessed.
5+
Similarly, when mob2 is used to invoke purchase, attribute values (Samsung and 3000) of mob2 are accessed in purchase().
6+
'''
7+
8+
class Mobile:
9+
def __init__(self, brand, price):
10+
print("Inside constructor")
11+
self.brand = brand
12+
self.price = price
13+
14+
def purchase(self):
15+
print("Purchasing a mobile")
16+
print("This mobile has brand", self.brand, "and price", self.price)
17+
18+
print("Mobile-1")
19+
mob1=Mobile("Apple", 20000)
20+
mob1.purchase()
21+
22+
print("Mobile-2")
23+
mob2=Mobile("Samsung",3000)
24+
mob2.purchase()

‎docs/OOPS/Classes and Objects.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Objects are real world entities. Anything you can describe in this world is an object.
2+
Classes on the other hand are not real. They are just a concept. Class is a short form of Classification.
3+
A class is a classification of certain objects and it is just a description of the properties and behavior all objects of that classification should possess.
4+
5+
Class is a like a recipe and the object is like the cupcake we bake using it.
6+
All cupcakes created from a recipe share similar characteristics like shape, sweetness, etc.
7+
But they are all unique as well. One cupcake may have strawberry frosting while another might have vanilla.
8+
Similarly, objects of a class share similar characteristics but they differ in their values for those characteristics.
9+
10+
A class is defined using the class keyword in python. For example, the below code creates a class called Mobile
11+
without any attributes or behavior.
12+
13+
class Mobile:
14+
pass
15+

‎docs/OOPS/Constructor1.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Mobile:
2+
def __init__(self, brand, price):
3+
print("Inside constructor")
4+
self.brand = brand
5+
self.price = price
6+
7+
mob1=Mobile("Apple", 20000)
8+
print("Mobile 1 has brand", mob1.brand, "and price", mob1.price)
9+
10+
mob2=Mobile("Samsung",3000)
11+
print("Mobile 2 has brand", mob2.brand, "and price", mob2.price)

‎docs/OOPS/Encapsulation.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
We can put a lock on that data by adding a double underscore in front of it.
3+
4+
Adding a double underscore makes the attribute a private attribute.
5+
Private attributes are those which are accessible only inside the class.
6+
This method of restricting access to our data is called encapsulation.
7+
'''
8+
9+
class Customer:
10+
def __init__(self, cust_id, name, age, wallet_balance):
11+
self.cust_id = cust_id
12+
self.name = name
13+
self.age = age
14+
self.__wallet_balance = wallet_balance
15+
16+
def update_balance(self, amount):
17+
if amount < 1000 and amount > 0:
18+
self.__wallet_balance += amount
19+
20+
def show_balance(self):
21+
print ("The balance is ",self.__wallet_balance)
22+
23+
c1=Customer(100, "Gopal", 24, 1000)
24+
print(c1.__wallet_balance)

‎docs/OOPS/Printing_objects.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
For a readable output when printing an object we can use the inbuilt special __str__ method.
3+
This method MUST return a string and this string will be used when the object is printed.
4+
This is useful in debugging as we can print the values of the attributes
5+
'''
6+
7+
class Shoe:
8+
def __init__(self, price, material):
9+
self.price = price
10+
self.material = material
11+
12+
def __str__(self):
13+
return "Shoe with price: " + str(self.price) + " and material: " + self.material
14+
15+
s1=Shoe(1000, "Canvas")
16+
print(s1)

‎docs/OOPS/Shopping_app.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Mobile:
2+
def __init__(self, brand, price):
3+
print("Inside the Mobile constructor")
4+
self.brand = brand
5+
self.price = price
6+
self.total_price = None
7+
8+
def purchase(self):
9+
if self.brand == "Apple":
10+
discount = 10
11+
else:
12+
discount = 5
13+
self.total_price = self.price - self.price * discount / 100
14+
print("Total price of", self.brand, "mobile is", self.total_price)
15+
16+
def return_product(self):
17+
print("Refund Amount for", self.brand, "mobile is", self.total_price)
18+
19+
class Shoe:
20+
def __init__(self, material, price):
21+
print("Inside the Shoe constructor")
22+
self.material = material
23+
self.price = price
24+
self.total_price = None
25+
26+
def purchase(self):
27+
if self.material == "leather":
28+
tax = 5
29+
else:
30+
tax = 2
31+
self.total_price = self.price + self.price * tax / 100
32+
print("Total price of", self.material, "shoe is", self.total_price)
33+
34+
def return_product(self):
35+
print("Refund Amount for", self.material, "shoe is", self.total_price)
36+
37+
mob1=Mobile("Apple", 20000)
38+
mob2=Mobile("Samsung", 10000)
39+
40+
shoe1=Shoe("leather",3000)
41+
shoe2=Shoe("canvas",200)
42+
43+
mob1.purchase()
44+
mob2.purchase()
45+
46+
shoe1.purchase()
47+
shoe2.purchase()
48+
49+
mob2.return_product()
50+
51+
shoe1.return_product()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /