-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticvariable.py
More file actions
71 lines (57 loc) · 1.65 KB
/
Copy pathstaticvariable.py
File metadata and controls
71 lines (57 loc) · 1.65 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# coding=utf-8
# ---------- STATIC VARIABLES ----------
# Fields declared in a class, but outside of any method
# are static variables. There value is shared by every
# object of that class
class Dog:
# This is a static variable
num_of_dogs = 0
def __init__(self, name="Unknown"):
self.name = name
# You reference the static variable by proceeding
# it with the class name
Dog.num_of_dogs += 1
@staticmethod
def getNumOfDogs():
print("There are currently {} dogs".format(Dog.num_of_dogs))
def main():
spot = Dog("Spot")
doug = Dog("Doug")
spot.getNumOfDogs()
doug.getNumOfDogs()
main()
#
# # ---------- MODULES ----------
# # Your Python programs will contain a main program that
# # includes your main function. Then you will create many
# # modules in separate files. Modules also end with .py
# # just like any other Python file
#
# # ————— sum.py —————
# def getSum(*args):
# sum = 0
#
# for i in args:
# sum += i
#
# return sum
#
# # ————— End of sum.py —————
#
# # You can import by listing the file name minus the py
# import sum
#
# # Get access to functions by proceeding with the file
# # name and then the function you want
# print("Sum :", sum.getSum(1,2,3,4,5))
#
# # ---------- FROM ----------
#
# # You can use from to copy specific functions from a module
# # You can use from sum import * to import all functions
# # You can import multiple functions by listing them after
# # import separated by commas
# from sum import getSum
#
# # You don't have to reference the module name now
# print("Sum :", getSum(1,2,3,4,5))