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 122bea0

Browse files
Class attributes
1 parent 3089317 commit 122bea0

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

‎Class-Attributes/class-attributes-1.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
# class-attributes-1.py
42

53
# Here we define an attribute under the class `YourClass`
@@ -17,9 +15,9 @@ def set_val(self):
1715

1816

1917
dd = YourClass()
20-
print(dd.classy) #10 # This will fetch the class attribute 10.
18+
dd.classy # This will fetch the class attribute 10.
2119
dd.set_val()
22-
print(dd.insty) #100 # This will fetch the instance attribute 100.
20+
dd.insty #100 # This will fetch the instance attribute 100.
2321

2422
# Once `dd` is instantiated, we can access both the class and instance
2523
# attributes, ie.. dd.classy and dd.insty.

‎Class-Attributes/class-attributes-2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
# class-attributes-2.py
42

53
# The code below shows two important points:
@@ -31,3 +29,10 @@ class YourClass(object):
3129
del dd.classy
3230
# Since the overriding attribute was deleted, this will print 'class value'.
3331
print(dd.classy)
32+
33+
'''
34+
O/P-
35+
class value
36+
Instance value
37+
class value
38+
'''

‎Class-Attributes/class-instance-attributes-1.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
# class-instance-attributes-1.py
42

53
# This code shows that an Instance can access it's own
@@ -34,3 +32,19 @@ def get_count(self):
3432
for obj in (a, b, c):
3533
print("value of obj: %s" % obj.get_val())
3634
print("Count : %s" % obj.get_count())
35+
36+
'''
37+
O/P-
38+
5
39+
value of obj: None
40+
3
41+
Count : None
42+
10
43+
value of obj: None
44+
3
45+
Count : None
46+
15
47+
value of obj: None
48+
3
49+
Count : None
50+
'''

0 commit comments

Comments
(0)

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