Hello, Explorer! Today, we're delving into the realm of Python's Object-Oriented Programming (OOP), focusing on class attributes and methods. Our goal is to grasp these concepts using clear, practical examples. Let's get started!
Class attributes are properties of class instances. For instance, a Spaceship
class could include attributes such as color, max speed, and fuel capacity. These are defined within the class block, as shown below:
Python1class Spaceship: 2 color = "white" 3 max_speed = 10000 4 fuel_capacity = 20000
These attributes can be accessed through a class instance, like so:
Python1enterprise = Spaceship() 2print(enterprise.color) # prints: white 3print(enterprise.max_speed) # prints: 10000
Class methods define a class's behavior. For example, a spaceship can fly. Methods are essentially functions within a class. The fly
method below enables an instance of the Spaceship
class to fly:
Python1class Spaceship: 2 color = "white" 3 fuel_capacity = 20000 4 5 def fly(self, speed): 6 return f"The {self.color} spaceship is flying! Speed = {speed}, fuel_capacity = {self.fuel_capacity}"
Note, that when referring to class attributes inside the class, you should always use the self
keyword that's always provided as a first parameter in every method of the class, similarly to how we used self.color
, and self.fuel_capacity
in the example above. At the same time, the provided parameter speed
is just used as is, as it's not a class attribute but an external parameter.
This fly
method can be invoked in the following manner:
Python1enterprise = Spaceship() 2print(enterprise.fly(10000)) # prints: The white spaceship is flying! Speed = 10000, fuel_capacity = 20000
Attributes and methods can be accessed and implemented through class instances. The attributes of a class can be set, and its methods can be called, as demonstrated below:
Python1enterprise = Spaceship() 2enterprise.color = "blue" # setting an attribute 3enterprise.fly() # calling a method
Different classes can interact with each other:
Python1class Star: 2 brightness = 5 3 4 def shine(self): 5 return self.brightness 6 7class Planet: 8 star = Star() 9 10 def get_light(self): 11 return self.star.shine() 12 13earth = Planet() 14print(earth.get_light()) # Prints: 5
See how the Planet
class created an attribute that is an instance of the Star
class and how we then use it in the get_light()
method in the Planet
class.
Constants are class attributes that remain unchanged. They are typically declared in capitalized letters, as follows:
Python1class Physics: 2 UNIVERSAL_GRAVITATIONAL_CONSTANT = 6.6743 * 10**(-11)
In this case, the UNIVERSAL_GRAVITATIONAL_CONSTANT
constant doesn't vary across instances of the Physics
class.
Congratulations! You now understand class attributes and methods. Continue exploring and anticipate our next Python adventure: the study of the __init__
method, inheritance, and more!
Today, we unraveled the details of class attributes and methods in Python. You've learned how to define them and bring Python objects to life. A hands-on exercise follows next. Remember, practice is the key to mastering these concepts. Happy coding!