-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatic_variable_LtP11.py
More file actions
43 lines (33 loc) · 1.17 KB
/
Static_variable_LtP11.py
File metadata and controls
43 lines (33 loc) · 1.17 KB
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
36
37
38
39
40
41
42
43
# a static variable is a variable created inside a class but
# outside of any other methods; the use of static variable is
# that for any object created using the class in which this
# variable has been set up, it will be the same
# create a dog class
class Dog:
# create a static variable - field outside of any method
num_of_dogs = 0
# initialize a class with init method with name
def __init__(self, name="Unknown"):
# assign name
self.name = name
# monitor each time the class will be used - increment
# static variable num_of_dogs; to use the variable user
# have o simply refer to it inside a class
Dog.num_of_dogs += 1
# create a static method that will print out the number
# of dogs that have been created
@staticmethod
def getNumOfDogs():
# printout the number of dogs
print("There are currently {} dogs.".format(
Dog.num_of_dogs))
# main function
def main():
# create a number of dogs
spot = Dog("Spot")
doug = Dog("Doug")
# the number of dogs stays the same for either of the 2 dogs
spot.getNumOfDogs()
doug.getNumOfDogs()
# call main
main()