A Basic Python Class Example

October 2020

A Quick Look

This is my quick look reminder of how Python classes are structured.

class Shark:

    # Class variables
    animal_type = "fish"
    location = "ocean"

    # Constructor method with instance variables name and age
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Method with instance variable followers
    def set_followers(self, followers):
        print("This user has " + str(followers) + " followers")


def main():
    # First object, set up instance variables of constructor method
    sammy = Shark("Sammy", 5)

    # Print out instance variable name
    print(sammy.name)

    # Print out class variable location
    print(sammy.location)

    # Second object
    stevie = Shark("Stevie", 8)

    # Print out instance variable name
    print(stevie.name)

    # Use set_followers method and pass followers instance variable
    stevie.set_followers(77)

    # Print out class variable animal_type
    print(stevie.animal_type)


if __name__ == "__main__":
    main()
Output:
Sammy
ocean
Stevie
This user has 77 followers
fish
end of line

Endnotes

I don't remember making this example. Feels like something I may have copy/pasted. That source has long since been forgotten if that waddBaseStyleSheetas the case.