Understanding Python OOP: Objects and Instance Variables

🚀 Understanding Object References and Instance Variables in Python Today I explored an interesting concept in Python Object-Oriented Programming (OOP) — how objects can be passed as input to functions, returned from functions, and assigned to another reference variable. 🔹 Key Concepts I Practiced: 1️⃣ Instance Variables Instance variables are variables defined inside a class using self. They belong to the object and store data specific to that object. 2️⃣ Object as Function Input In Python, an object can be passed to a function just like a normal variable. The function receives the reference of the object. 3️⃣ Returning an Object from a Function A function can also create and return a new object, which can then be stored in another reference variable. 4️⃣ Multiple References to the Same Object When one object is assigned to another variable, both variables refer to the same object in memory. Changing the value through one reference affects the other. 💻 Example Code class Person: def __init__(self, salary, empid): self.salary = salary self.empid = empid def greet(person): print("Salary:", person.salary, "EmpID:", person.empid) # Creating a new object new_person = Person(550000, 102) return new_person # Creating object p = Person(100000, 101) # Passing object to function x = greet(p) # Assigning returned object to another reference y = x # Printing values using another reference print(y.salary) print(y.empid) 📌 What I Learned Objects are passed by reference in Python Instance variables store object-specific data Functions can return objects Multiple variables can refer to the same object Learning these small concepts step by step makes understanding Python OOP much easier. #Python #OOP #Programming #LearningPython #DeveloperJourney

To view or add a comment, sign in

Explore content categories