Index
Private Attributes and Methods in Python
Concept of Private and Public in Python Classes
Understanding 3 Types of Functions in OOP
Class Method in Python
Impact of Class Attribute Changes on Object Attributes
1. Private Attributes and Methods in Python
Definition:
Private attributes and methods are those that cannot be accessed directly outside the class. In Python, private attributes are defined by prefixing the variable or method name with double underscores (__
).
Use-Case Example:
Private attributes and methods are useful when you want to hide the internal data and logic of a class from external code to prevent unintended manipulation.
Syntax:
class MyClass:
def __init__(self):
self.__private_attribute = "I am private"
def __private_method(self):
return "This is a private method"
Rules:
Private attributes and methods are only accessible within the class.
To access or modify them, you need to use a public method inside the class.
Basic Example:
class Car:
def __init__(self, model):
self.__model = model # Private attribute
def get_model(self):
return self.__model # Accessing private attribute
car = Car("Toyota")
print(car.get_model()) # Output: Toyota
# print(car.__model) # This would raise an AttributeError
2. Concept of Private and Public in Python Classes
Definition:
In Python, attributes and methods can be either public or private. Public attributes and methods are accessible from outside the class, while private attributes and methods are restricted to within the class.
Use-Case Example:
Use private attributes for sensitive data or implementation details you don’t want exposed. Public attributes can be accessed or modified by other parts of the program.
Syntax:
class MyClass:
public_attribute = "I am public"
__private_attribute = "I am private"
Rules:
By default, all attributes and methods in Python are public.
Prefixing an attribute or method with
__
makes it private.
Basic Example:
class Person:
def __init__(self, name):
self.name = name # Public attribute
self.__age = 30 # Private attribute
person = Person("Alice")
print(person.name) # Output: Alice
# print(person.__age) # Raises AttributeError due to private attribute
3. Understanding 3 Types of Functions in OOP
Definition:
In object-oriented programming, functions within a class can be categorized into:
Instance Methods: Operate on instance attributes and require
self
as the first parameter.Class Methods: Operate on class attributes and use
@classmethod
decorator, withcls
as the first parameter.Static Methods: Perform general utility tasks and do not require
self
orcls
.
Use-Case Example:
Each function type serves a unique purpose: instance methods for object-specific data, class methods for class-level data, and static methods for utility operations.
Syntax:
class MyClass:
def instance_method(self):
pass
@classmethod
def class_method(cls):
pass
@staticmethod
def static_method():
pass
Basic Example:
class MathOperations:
def add(self, a, b): # Instance method
return a + b
@classmethod
def get_class_name(cls): # Class method
return cls.__name__
@staticmethod
def greet(): # Static method
return "Hello, welcome to MathOperations!"
math = MathOperations()
print(math.add(5, 3)) # Output: 8
print(MathOperations.get_class_name()) # Output: MathOperations
print(MathOperations.greet()) # Output: Hello, welcome to MathOperations!
4. Class Method in Python
Definition:
A class method is a method that operates on the class rather than the instance. It can modify the class state shared among all instances. Class methods use the @classmethod
decorator and take cls
as the first parameter.
Use-Case Example:
Class methods are used when you need to work with class-level data or create alternative constructors.
Syntax:
class MyClass:
@classmethod
def class_method(cls):
pass
Basic Example:
class Employee:
company_name = "TechCorp"
@classmethod
def change_company(cls, new_name):
cls.company_name = new_name
# Change company name for all instances
Employee.change_company("NewTech")
print(Employee.company_name) # Output: NewTech
5. Impact of Class Attribute Changes on Object Attributes
Definition:
Class attributes are shared by all instances of a class, while instance attributes are unique to each instance. Changes to a class attribute affect all instances unless an instance attribute of the same name overrides it.
Use-Case Example:
This is useful when you want a shared attribute among instances but also want the flexibility for instances to have their own specific values.
Syntax:
class MyClass:
class_attribute = "I am shared"
Basic Example:
class Car:
brand = "Toyota" # Class attribute
# Creating two instances
car1 = Car()
car2 = Car()
print(car1.brand) # Output: Toyota
print(car2.brand) # Output: Toyota
# Changing class attribute
Car.brand = "Honda"
print(car1.brand) # Output: Honda
print(car2.brand) # Output: Honda
# Assigning instance attribute to one instance
car1.brand = "Ford"
print(car1.brand) # Output: Ford
print(car2.brand) # Output: Honda
Yours, Vishal Rajput.