Melissa
0
Q:

class variable in python

Python is object-oriented.
This just mean that you can create classes and objects.
This is a good thing! :D
Being object-oriented lets coders code faster and more efficient!
2
class Shark:
    animal_type = "fish"
    location = "ocean"
    followers = 5

new_shark = Shark()
print(new_shark.animal_type)
print(new_shark.location)
print(new_shark.followers)
0
# Class variables refer to variables that are made within a class.
# It is generated when you define the class.
# It's shared with all the instance of that class.
# Example:

class some_variable_holder(object):
    
    var = "This variable is created inside the class some_variable_holder()."
    
    def somefunc(self):
        print("Random function ran.")

thing = some_variable_holder()
another_thing = some_variable_holder()

# Both does the same thing because the same variable has been passed on from the class.
thing.var
another_thing.var
0

New to Communities?

Join the community