clubs:python_club:python_club_objects_classes
Home | clubs :: cloud club :: python_club :: 3D-Printing | projects :: Proxmox | Kubernetes | scripting | utilities | games
Table of Contents
Python Club Topics - Classes & Objects
Questions
- What a class? A class is like a blueprint or template for creating objects. It defines the attributes (data) and methods (actions) that objects of that class will have. It is a logical entity and doesn't occupy memory space when declared.
- What an object? An object is an instance of a class. It's a concrete entity that exists in memory and has specific values for the attributes defined by its class. It's a physical entity that can be manipulated.
Class
- A class is a blueprint for creating objects
Class examples
- Define a basic class
class Dog: def __init__(self, name, breed): self.name = name self.breed = breed # Create an instance of the Dog class my_dog = Dog("Buddy", "Golden Retriever") # Access the attributes of the object print(my_dog.name) print(my_dog.breed)
- You cannot print an object directly like a list, tuple, or dictionary
class Dog: def __init__(self, name, breed): self.name = name self.breed = breed # Create an instance of the Dog class my_dog = Dog("Buddy", "Golden Retriever") # Access the attributes of the object print(my_dog)
- Instead, you print all values of an object with the _ _dict_ _ method
class Dog: def __init__(self, name, breed): self.name = name self.breed = breed # Create an instance of the Dog class my_dog = Dog("Buddy", "Golden Retriever") print(my_dog.__dict__)
clubs/python_club/python_club_objects_classes.txt · Last modified: by 127.0.0.1
