Skip to content

Commit bc6cce0

Browse files
update file
1 parent 0c21c7a commit bc6cce0

File tree

4 files changed

+479
-0
lines changed

4 files changed

+479
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# How object access attributes in OOP"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"class Person:\n",
17+
" def __init__(self,name, country):\n",
18+
" self.name = name\n",
19+
" self.country = country\n",
20+
"\n",
21+
" def greeting(self):\n",
22+
" if self.country == \"india\":\n",
23+
" print(f\"Namaste {self.name}\")\n",
24+
" \n",
25+
" else:\n",
26+
" print(f\"Hello {self.name}\")\n"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 4,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"p = Person(\"Bappy\", \"USA\")"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 5,
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"Hello Bappy\n"
48+
]
49+
}
50+
],
51+
"source": [
52+
"p.greeting()"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 6,
58+
"metadata": {},
59+
"outputs": [
60+
{
61+
"data": {
62+
"text/plain": [
63+
"'Bappy'"
64+
]
65+
},
66+
"execution_count": 6,
67+
"metadata": {},
68+
"output_type": "execute_result"
69+
}
70+
],
71+
"source": [
72+
"p.name"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 7,
78+
"metadata": {},
79+
"outputs": [
80+
{
81+
"data": {
82+
"text/plain": [
83+
"'USA'"
84+
]
85+
},
86+
"execution_count": 7,
87+
"metadata": {},
88+
"output_type": "execute_result"
89+
}
90+
],
91+
"source": [
92+
"p.country"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 8,
98+
"metadata": {},
99+
"outputs": [
100+
{
101+
"ename": "AttributeError",
102+
"evalue": "'Person' object has no attribute 'gender'",
103+
"output_type": "error",
104+
"traceback": [
105+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
106+
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
107+
"Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m p\u001b[38;5;241m.\u001b[39mgender\n",
108+
"\u001b[1;31mAttributeError\u001b[0m: 'Person' object has no attribute 'gender'"
109+
]
110+
}
111+
],
112+
"source": [
113+
"p.gender"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 9,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"# Attribute creation from outside of the class\n",
123+
"\n",
124+
"p.gender = \"male\""
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 10,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/plain": [
135+
"'male'"
136+
]
137+
},
138+
"execution_count": 10,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
143+
"source": [
144+
"p.gender"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 11,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"p.age = 23"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 12,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"data": {
163+
"text/plain": [
164+
"23"
165+
]
166+
},
167+
"execution_count": 12,
168+
"metadata": {},
169+
"output_type": "execute_result"
170+
}
171+
],
172+
"source": [
173+
"p.age"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {},
180+
"outputs": [],
181+
"source": []
182+
}
183+
],
184+
"metadata": {
185+
"kernelspec": {
186+
"display_name": "base",
187+
"language": "python",
188+
"name": "python3"
189+
},
190+
"language_info": {
191+
"codemirror_mode": {
192+
"name": "ipython",
193+
"version": 3
194+
},
195+
"file_extension": ".py",
196+
"mimetype": "text/x-python",
197+
"name": "python",
198+
"nbconvert_exporter": "python",
199+
"pygments_lexer": "ipython3",
200+
"version": "3.12.4"
201+
}
202+
},
203+
"nbformat": 4,
204+
"nbformat_minor": 2
205+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Pass by reference in OOP"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 2,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"class Person:\n",
17+
"\n",
18+
" def __init__(self,name,gender):\n",
19+
" self.name = name\n",
20+
" self.gender = gender\n",
21+
"\n",
22+
"\n",
23+
"\n",
24+
"#outside of class\n",
25+
"def greeting(person):\n",
26+
" print(f\"Hi My name is {person.name} and i am a {person.gender}\")\n",
27+
" p1 = Person(\"Alex\", \"Male\")\n",
28+
" return p1"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 3,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"p = Person(\"Bappy\", \"Male\")"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 4,
43+
"metadata": {},
44+
"outputs": [
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"Hi My name is Bappy and i am a Male\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"a = greeting(p)"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 7,
60+
"metadata": {},
61+
"outputs": [
62+
{
63+
"data": {
64+
"text/plain": [
65+
"'Alex'"
66+
]
67+
},
68+
"execution_count": 7,
69+
"metadata": {},
70+
"output_type": "execute_result"
71+
}
72+
],
73+
"source": [
74+
"a.name"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 9,
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"data": {
84+
"text/plain": [
85+
"'Male'"
86+
]
87+
},
88+
"execution_count": 9,
89+
"metadata": {},
90+
"output_type": "execute_result"
91+
}
92+
],
93+
"source": [
94+
"a.gender"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"metadata": {},
101+
"outputs": [],
102+
"source": []
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "base",
108+
"language": "python",
109+
"name": "python3"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 3
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython3",
121+
"version": "3.12.4"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 2
126+
}

0 commit comments

Comments
 (0)