Skip to content

Methods and Attributes

Now that you know what a class is and how to create objects, it’s time to delve into two key concepts: attributes and methods. These are the fundamental components that give functionality and characteristics to your objects.


Attributes are variables that store information about the state of an object. Each object can have different values for its attributes.

Instance Attributes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person('Ana', 30)
print(p1.name) # Ana
print(p1.age) # 30

In this example, name and age are attributes of each Person object.


Methods are functions defined within a class that describe the behaviors of objects. They always receive at least one parameter: self, which represents the object itself.

Instance Method
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print(f'Hello, I am {self.name}!')
p1 = Person('Ana')
p1.greet() # Hello, I am Ana!

  • Instance attributes: Are unique to each object (like self.name).
  • Class attributes: Are shared by all instances of the class.
Class and Instance Attributes
class Pet:
species = 'Dog' # Class attribute
def __init__(self, name):
self.name = name # Instance attribute
m1 = Pet('Rex')
m2 = Pet('Luna')
print(m1.species) # Dog
print(m2.species) # Dog

  • Instance methods: Use self and operate on the object.
  • Class methods: Use @classmethod and receive cls as the first parameter.
  • Static methods: Use @staticmethod and receive neither self nor cls.
Static and Class Methods
class Calculator:
@staticmethod
def add(a, b):
return a + b
@classmethod
def description(cls):
return 'Simple calculator'
print(Calculator.add(3, 4)) # 7
print(Calculator.description()) # Simple calculator

  • Use descriptive names for attributes and methods.
  • Private attributes (for internal use only) are named with an underscore: _attribute.
  • Document your methods using docstrings.

  • Attributes: store object information.
  • Methods: define object behaviors.
  • There are instance, class, and static attributes and methods.
  • Mastering these concepts allows you to create more useful and flexible classes.

Ready to continue? Try creating a class with your own attributes and methods!