-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path12_classes_objects.py
More file actions
61 lines (48 loc) · 1.53 KB
/
Copy path12_classes_objects.py
File metadata and controls
61 lines (48 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
12_classes_objects.py
Purpose: classes, instances, attributes, methods, inheritance, __repr__, encapsulation basics.
"""
def h(t): print("\n---", t, "---\n")
h("Basic class and instance")
class Person:
"""Simple person class."""
species = "Homo sapiens" # class attribute
def __init__(self, name, age):
self.name = name # instance attribute
self.age = age
def introduce(self):
return f"My name is {self.name}, age {self.age}"
def __repr__(self):
return f"Person(name={self.name!r}, age={self.age!r})"
p = Person("Khushal", 25)
print(p.introduce())
print("repr:", repr(p))
print("species:", Person.species)
h("Encapsulation and 'private' attributes")
class Counter:
def __init__(self):
self._value = 0 # single underscore: internal by convention
def inc(self):
self._value += 1
def value(self):
return self._value
c = Counter()
c.inc(); c.inc()
print("Counter value:", c.value())
h("Inheritance")
class Student(Person):
def __init__(self, name, age, sid):
super().__init__(name, age)
self.sid = sid
def introduce(self):
base = super().introduce()
return f"{base} (student id: {self.sid})"
s = Student("Aisha", 20, "S123")
print(s.introduce())
h("Polymorphism example")
objs = [p, s]
for o in objs:
print(o.introduce())
h("Mini Exercises")
# 1) Implement a BankAccount class with deposit, withdraw (prevent negative balance), and __repr__.
# 2) Implement classmethod/staticmethod examples and explain use-cases.