In Python, instance attributes are variables that belong to a specific object created from a class. They are usually defined inside the __init__ method using self, and each object has its own independent copy of these attributes. This makes them different from class attributes, which are shared across all instances.
class Car:
def __init__(self, brand, model):
# Instance attributes
self.brand = brand
self.model = model
car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Civic")
print(car1.brand, car1.model) # Toyota Camry
print(car2.brand, car2.model) # Honda Civic
class Dog:
def __init__(self, name):
self.name = name
self.legs = 4
def speak(self):
print(self.name + ' says: Bark!')
myDog = Dog('Rover')
print(myDog.name)
print(myDog.legs)
Class Definition
You define a class Dog with an __init__ method and a speak method.
__init__ Method
Runs automatically when you create a new Dog object.
self.name = name → assigns the given name to the instance.
self.legs = 4 → every dog object starts with 4 legs.
Instance Attributes
self.name and self.legs are instance attributes.
Each Dog object has its own copy of these attributes.
Creating an Object
myDog = Dog('Rover') → creates a new Dog instance with name = "Rover" and legs = 4.
Accessing Attributes
print(myDog.name) → prints "Rover".
print(myDog.legs) → prints 4.
Method Usage
If you call myDog.speak(), it will print:
Rover says: Bark!
adorable: very attractive and easy to feel love for
Static attributes in Python are what we usually call class attributes. Unlike instance attributes (unique to each object), static/class attributes are shared across all instances of the class. They are defined directly inside the class body, not inside __init__.
connotation: an idea suggested by a word in addition to its main meaning
In Python, a getter function is a method that allows you to safely access (or "get") the value of an attribute from a class. It’s often used together with properties to control how attributes are read, especially when you want to add validation, formatting, or encapsulation.
Encapsulation in Python is the practice of bundling data (attributes) and the methods (functions) that operate on that data into a single unit, typically a class. Its primary goal is to hide internal implementation details and restrict direct access to an object's state to prevent accidental modifications.
class Dog:
_legs = 4 # static/class attribute
def __init__(self, name):
self.name = name # instance attribute
def getLegs(self): # getter function
return self._legs
def speak(self):
print(self.name + ' says: Bark!')
myDog = Dog('Rover')
print(myDog.name) # Rover
print(myDog.getLegs()) # 4
In your example, you’ve introduced a static (class) attribute _legs and a getter function getLegs() to access it.
Static/Class Attribute (_legs)
Defined directly in the class body (_legs = 4).
Shared across all Dog objects.
Accessible via Dog._legs or through an instance (myDog._legs).
Instance Attribute (name)
Defined inside __init__ using self.name.
Unique to each object (e.g., Rover, Buddy, Max).
Getter Function (getLegs)
Provides controlled access to the static attribute.
Returns self._legs, which refers to the class-level _legs.
myDog = Dog('Rover')
myDog._legs = 3
print(myDog.name)
print(myDog.getLegs())
print(Dog._legs)
Class Attribute _legs = 4
Defined at the class level.
Shared by all Dog objects unless shadowed.
Instance Creation
myDog = Dog('Rover') → creates a new dog with name = "Rover".
At this point, myDog sees _legs from the class (Dog._legs = 4).
Assigning myDog._legs = 3
This does not change the class attribute.
Instead, it creates a new instance attribute _legs on myDog only.
Now myDog has its own _legs = 3, shadowing the class’s _legs.
Printing Values
print(myDog.name) → "Rover" (instance attribute).
print(myDog.getLegs()) → 3 (because self._legs now refers to the instance attribute).
print(Dog._legs) → 4 (class attribute remains unchanged).
Microsoft Copilot