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 f988008

Browse files
CLASS
1 parent 2ef5d4d commit f988008

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python2.7
2+
3+
# class-attributes-1.py
4+
5+
# Here we define an attribute under the class `YourClass`
6+
# as well as an attribute within the function.
7+
8+
# The attribute defined in the class is called `class attributes`
9+
# and the attribute defined in the function is called `instance attributes`.
10+
11+
12+
class YourClass(object):
13+
classy = 10
14+
15+
def set_val(self):
16+
self.insty = 100
17+
18+
19+
dd = YourClass()
20+
dd.classy # This will fetch the class attribute 10.
21+
dd.set_val()
22+
dd.insty # This will fetch the instance attribute 100.
23+
24+
# Once `dd` is instantiated, we can access both the class and instance
25+
# attributes, ie.. dd.classy and dd.insty.

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
# class-attributes-2.py
4+
5+
# The code below shows two important points:
6+
7+
# a) A class attribute can be overridden in an instance, even
8+
# though it is bad due to breaking Encapsulation.
9+
10+
# b) There is a lookup path for attributes in Python. The first being
11+
# the method defined within the class, and then the class above it.
12+
13+
# We are overriding the 'classy' class attribute in the instance 'dd'.
14+
# When it's overridden, the python interpreter reads the overridden value.
15+
# But once the new value is deleted with 'del', the overridden value is no longer
16+
# present in the instance, and hence the lookup goes a level above and gets it from
17+
# the class.
18+
19+
20+
class YourClass(object):
21+
classy = "class value"
22+
23+
24+
dd = YourClass()
25+
print(dd.classy) # < This should return the string "class value"
26+
27+
dd.classy = "Instance value"
28+
print(dd.classy) # This should return the string "Instance value"
29+
30+
# This will delete the value set for 'dd.classy' in the instance.
31+
del dd.classy
32+
# Since the overriding attribute was deleted, this will print 'class value'.
33+
print(dd.classy)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
# class-instance-attributes-1.py
4+
5+
# This code shows that an Instance can access it's own
6+
# attributes as well as Class attributes.
7+
8+
# We have a class attribute named 'count', and we add 1 to
9+
# it each time we create an instance. This can help count the
10+
# number of instances at the time of instantiation.
11+
12+
13+
class InstanceCounter(object):
14+
count = 0
15+
16+
def __init__(self, val):
17+
self.val = val
18+
InstanceCounter.count += 1
19+
20+
def set_val(self, newval):
21+
self.val = newval
22+
23+
def get_val(self):
24+
print(self.val)
25+
26+
def get_count(self):
27+
print(InstanceCounter.count)
28+
29+
30+
a = InstanceCounter(5)
31+
b = InstanceCounter(10)
32+
c = InstanceCounter(15)
33+
34+
for obj in (a, b, c):
35+
print("value of obj: %s" % obj.get_val())
36+
print("Count : %s" % obj.get_count())

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ In Python, classes are defined by the "Class" keyword
3030
```
3131
class myClass():
3232
```
33+
[Class Attributes]()
3334
------------
3435
------------
3536

0 commit comments

Comments
(0)

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