python / beginner
Snippet
Defining a Basic Class
A class is a blueprint for creating objects. You can define attributes inside a class that all instances will share.
snippet.py
1
2
3
4
5
class Dog:species = "Canine"my_dog = Dog()print(my_dog.species)
Breakdown
1
class Dog:
Defines a new class named Dog.
2
species = "Canine"
Creates a class attribute named species.
3
my_dog = Dog()
Creates an instance (object) of the Dog class.
4
print(my_dog.species)
Accesses and prints the attribute from the object.