Object oriented Programming
- Lets try to build an application which does calcuations i.e. we will be builing a calculator.
- Investment Calculator
- Loan Calculator
- Tax Caclulator
Basics and Syntax
- Refer Here for programiz class
- How to define a class
class <class-name>:
....
....
....
- Class names should be Pascal Casing
class Car:
pass
- Class can have members.
class Car:
type = "hatchback"
model = "Civic"
manufacturer = "Honda"
- note: we have member types, which we will be revisiting soon
- Class can have methods
class Car:
type = "hatchback"
model = "Civic"
manufacturer = "Honda"
def start():
pass
def stop():
pass
- Class has three types of methods
- Static methods
- class methods
- object/instance methods
- Lets use object methods and object members for some time and revisit class methods
- Let revisit syntaxes with object/instance methods and members
- lets look into a method called
__init__ - Refer Here for class methods, class members, instance methods, instance members and static methods
- Terms introduced
- decorator
@classmethod
- decorator
Inheritence in Python
- For inheritence and access modifier refer jupyter notebook
Summary of Public, Private, and Protected in Python
Public, private, and protected are access modifiers in Python used to control the visibility and accessibility of class members (attributes and methods). Python uses naming conventions, not strict enforcement, to indicate these levels of access[5][3][1].
Public Members
– Definition: Accessible from anywhere-inside or outside the class.
– Syntax: No underscore prefix.
– Example: self.name
– Behavior: Default for all class variables and methods.
– Usage: Can be freely accessed and modified from any part of the code[1][2][3][5].
Protected Members
– Definition: Intended to be accessible within the class and its subclasses.
– Syntax: Prefix with a single underscore (_), e.g., self._age
– Behavior: This is a convention-Python does not prevent outside access, but it signals that the member is meant for internal or subclass use.
– Usage: Used when you want to allow subclass access but discourage external use[1][3][4][5][6].
Private Members
– Definition: Intended to be accessible only within the defining class.
– Syntax: Prefix with double underscores (__), e.g., self.__salary
– Behavior: Python applies name mangling-the interpreter changes the name of the variable to include the class name, making accidental access from outside the class more difficult.
– Usage: Accessing from outside the class raises an AttributeError, but it can still be accessed using the mangled name (e.g., _ClassName__salary), though this is discouraged[1][3][5][6].
At a Glance
| Modifier | Prefix | Accessible From | Enforced by Python? | Example |
|---|---|---|---|---|
| Public | (none) | Anywhere | No | self.name |
| Protected | _ | Class & subclasses | No (convention) | self._age |
| Private | __ | Class only (name mangling) | Partial (mangling) | self.__salary |
Key Points:
– Python’s access modifiers are conventions, not strict rules.
– Public: No underscores, accessible everywhere.
– Protected: Single underscore, intended for class and subclasses.
– Private: Double underscore, name mangling to discourage outside access[3][5][1].
– These conventions help with code organization, encapsulation, and maintainability.
Citations:
[1] https://www.tutorialspoint.com/access-modifiers-in-python-public-private-and-protected
[2] https://www.tutorialspoint.com/python/python_access_modifiers.htm
[3] https://dev.to/ankitmalikg/python-how-to-define-public-private-and-protected-variables-in-a-class-4g9
[4] https://www.tutorialsteacher.com/python/public-private-protected-modifiers
[5] https://www.scaler.com/topics/access-modifiers-in-python/
[6] https://llego.dev/posts/access-modifiers-python/
[7] https://prepinsta.com/python/access-modifiers/
[8] https://diveintopython.org/learn/classes/methods
[9] https://geekpython.in/access-modifiers-in-python
[10] https://technogeekscs.com/access-modifiers-in-python/
[11] https://www.datacamp.com/tutorial/python-private-methods-explained
[12] https://www.reddit.com/r/programming/comments/10h1n9x/public_private_and_protected_access_modifiers_in/
[13] https://www.designgurus.io/answers/detail/what-is-the-difference-between-public-private-and-protected
[14] https://www.studytonight.com/python/access-modifier-python
[15] https://pythonlobby.com/access-specifiers-or-access-modifiers-in-python-programming-public-private-and-protected-keywords/
Answer from Perplexity: pplx.ai/share
