-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument5.rtf
More file actions
136 lines (136 loc) · 5.6 KB
/
Copy pathDocument5.rtf
File metadata and controls
136 lines (136 loc) · 5.6 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.10586}\viewkind4\uc1
\pard\sa200\sl276\slmult1\f0\fs22\lang9 Classes and Objects Review\par
Python Classes and Objects\par
Class Definition: A class is a blueprint for creating objects. It defines the behavior an object will have through its attributes and methods. Here is a basic example of a class definition in Python:\par
Example Code\par
class Dog:\par
def __init__(self, name, age):\par
self.name = name\par
self.age = age\par
\par
def bark(self):\par
print(f'\{self.name.upper()\} says woof woof!')\par
Creating Objects: Objects are instances of a class. They are created by calling the class with the necessary arguments.\par
Example Code\par
dog1 = Dog('Jack', 3)\par
dog2 = Dog('Thatcher', 5)\par
\par
dog1.bark() # JACK says woof woof!\par
dog2.bark() # THATCHER says woof woof!\par
Calling Methods on Objects: You can call methods on objects to perform actions or retrieve information.\par
Example Code\par
object_name1.method_name()\par
object_name2.method_name()\par
Difference Between Class and Object: A class is a reusable template, while an object is a specific instance of that class with actual data.\par
Attributes\par
Instance Attributes: Defined in __init__() using self, and unique to each object.\par
Class Attributes: Defined directly inside the class and shared by all instances.\par
Example Code\par
class Dog:\par
species = 'French Bulldog' # Class attribute\par
\par
def __init__(self, name):\par
self.name = name # Instance attribute\par
\par
print(Dog.species) # French Bulldog\par
\par
jack = Dog('Jack')\par
print(jack.name) # Jack\par
print(jack.species) # French Bulldog\par
Methods\par
Methods: Functions defined inside a class that operate on the object's attributes.\par
Example Code\par
class Car:\par
def __init__(self, color, model):\par
self.color = color\par
self.model = model\par
\par
def describe(self):\par
return f'This car is a \{self.color\} \{self.model\}'\par
\par
my_car_1 = Car('red', 'Tesla Model S')\par
print(my_car_1.describe()) # This car is a red Tesla Model S\par
Accessing Methods: Call methods on objects using the dot notation. Here is an example of calling the describe method on two different car objects:\par
Example Code\par
class Car:\par
def __init__(self, color, model):\par
self.color = color \par
self.model = model \par
\par
def describe(self):\par
return f'This car is a \{self.color\} \{self.model\}'\par
\par
my_car_1 = Car('red', 'Tesla Model S')\par
my_car_2 = Car('green', 'Lamborghini Revuelto')\par
\par
print(my_car_1.describe()) # Calling method using the dot notation\par
\par
print(my_car_2.describe()) # Calling method using the dot notation\par
Dunder (Magic) Methods\par
Definition: Special methods that start and end with a double underscore (e.g., __init__, __len__, __str__, __eq__). Python uses them internally for built-in operations.\par
Example Code\par
class Book:\par
def __init__(self, title, pages):\par
self.title = title\par
self.pages = pages\par
\par
def __len__(self):\par
return self.pages\par
\par
def __str__(self):\par
return f"'\{self.title\}' has \{self.pages\} pages"\par
\par
def __eq__(self, other):\par
return self.pages == other.pages\par
\par
book1 = Book('Built Wealth Like a Boss', 420)\par
print(len(book1)) # 420\par
print(str(book1)) # 'Built Wealth Like a Boss' has 420 pages\par
Calling dunder methods indirectly: You don't need to call dunder methods directly. Instead, Python automatically calls them when certain actions happen. These operations include:\par
\par
arithmetic operations like addition, subtraction, multiplication, division, and others. __add__() is called for addition, __sub__() for subtraction, __mul__() for multiplication, and __truediv__() for division.\par
\par
string operations like concatenation, repetition, formatting, and conversion to text. __add__() is called for concatenation, __mul__() for repetition, __format__() for formatting, __str__() and __repr__() for text conversion, and so on.\par
\par
comparison operations like equality, less-than, greater-than, and others. __eq__() is called for equality checks, __lt__() for less-than, __gt__() for greater-than, and so on.\par
\par
iteration operations like making an object iterable and advancing through items. __iter__() is called to return an iterator and __next__() to fetch the next item.\par
\par
Real World Example: Shopping Cart\par
Cart Class with Dunder Methods: Allows adding, removing, iterating, and checking contents with built-in behavior.\par
Example Code\par
class Cart:\par
def __init__(self):\par
self.items = []\par
\par
def add(self, item):\par
self.items.append(item)\par
\par
def remove(self, item):\par
if item in self.items:\par
self.items.remove(item)\par
else:\par
print(f'\{item\} is not in cart')\par
\par
def list_items(self):\par
return self.items\par
\par
def __len__(self):\par
return len(self.items)\par
\par
def __getitem__(self, index):\par
return self.items[index]\par
\par
def __contains__(self, item):\par
return item in self.items\par
\par
def __iter__(self):\par
return iter(self.items)\par
\par
cart = Cart()\par
cart.add('Laptop')\par
print(len(cart)) # 1\par
print('Laptop' in cart) # True\par
}