File tree 3 files changed +25
-8
lines changed
3 files changed +25
-8
lines changed Original file line number Diff line number Diff line change 1
-
2
-
3
1
# class-attributes-1.py
4
2
5
3
# Here we define an attribute under the class `YourClass`
@@ -17,9 +15,9 @@ def set_val(self):
17
15
18
16
19
17
dd = YourClass ()
20
- print ( dd .classy ) #10 # This will fetch the class attribute 10.
18
+ dd .classy # This will fetch the class attribute 10.
21
19
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.
23
21
24
22
# Once `dd` is instantiated, we can access both the class and instance
25
23
# attributes, ie.. dd.classy and dd.insty.
Original file line number Diff line number Diff line change 1
- #!/usr/bin/env python
2
-
3
1
# class-attributes-2.py
4
2
5
3
# The code below shows two important points:
@@ -31,3 +29,10 @@ class YourClass(object):
31
29
del dd .classy
32
30
# Since the overriding attribute was deleted, this will print 'class value'.
33
31
print (dd .classy )
32
+
33
+ '''
34
+ O/P-
35
+ class value
36
+ Instance value
37
+ class value
38
+ '''
Original file line number Diff line number Diff line change 1
- #!/usr/bin/env python
2
-
3
1
# class-instance-attributes-1.py
4
2
5
3
# This code shows that an Instance can access it's own
@@ -34,3 +32,19 @@ def get_count(self):
34
32
for obj in (a , b , c ):
35
33
print ("value of obj: %s" % obj .get_val ())
36
34
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
+ '''
You can’t perform that action at this time.
0 commit comments