FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-examples/ObjectOrientedPython/InstanceVariables.py at master · dsabhrawal/python-examples · GitHub
dsabhrawal
/
python-examples
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-examples
/
ObjectOrientedPython
/
InstanceVariables.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (27 loc) · 1.08 KB
Breadcrumbs
python-examples
/
ObjectOrientedPython
/
InstanceVariables.py
Copy path
File metadata and controls
35 lines (27 loc) · 1.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Instance variables should be initialized before use
# There are three ways to initialize instance variables
# 1. Using Constructor 2. Using object 3. Using methods
class
Student
:
#Initializing using constructor
def
__init__
(
self
,
name
):
super
().
__init__
()
self
.
name
=
name
#Initializing using methods
def
defineAttributes
(
self
,
roll_no
,
age
):
self
.
roll_no
=
roll_no
self
.
age
=
age
def
display
(
self
):
print
(
'Name: '
,
self
.
name
)
print
(
'RollNo: '
,
self
.
roll_no
)
print
(
'Age: '
,
self
.
age
)
print
(
'Section: '
,
self
.
section
)
s
=
Student
(
'Student_A'
)
#only name is initialized
#s.display() #will result into error
s
.
section
=
'B'
#section is initialized
s
.
defineAttributes
(
113
,
15
)
#roll no and age is initialized
s
.
display
()
#Symbol table or special attribute __dict__ (holds dictionary of the instance variables)
print
(
s
.
__dict__
)
#Output: {'name': 'Student_A', 'section': 'B', 'roll_no': 113, 'age': 15}
#Deleting instance variables
del
s
.
age
print
(
s
.
__dict__
)
#Output: {'name': 'Student_A', 'section': 'B', 'roll_no': 113}
Back
|
FazBrowse Home
|
New Git URL