1class Car: wheels = 4 # <- Class variable def __init__(self, name): self.name = name # <- Instance variable
1# Class variables refer to variables that are made within a class.
2# It is generated when you define the class.
3# It's shared with all the instance of that class.
4# Example:
5
6class some_variable_holder(object):
7
8 var = "This variable is created inside the class some_variable_holder()."
9
10 def somefunc(self):
11 print("Random function ran.")
12
13thing = some_variable_holder()
14another_thing = some_variable_holder()
15
16# Both does the same thing because the same variable has been passed on from the class.
17thing.var
18another_thing.var
1class Car:
2 wheels = 4 # <- Class variable
3
4 def __init__(self, name):
5 self.name = name # <- Instance variable