R
Rishtaara
Python: Zero to Professional
Lesson 16 of 40Article18 min

Properties, Methods & Inheritance

Properties are attributes on objects — set in __init__ or assigned later.

Python Class Properties

Properties are attributes on objects — set in __init__ or assigned later.

Use @property for computed or validated fields (e.g. read-only balance).

Real-life example: Properties are labels on storage boxes — owner, balance, status — visible without opening everything.

@property example
class Circle:
    def __init__(self, radius):
        self.radius = radius

    @property
    def area(self):
        return 3.14 * self.radius ** 2

c = Circle(5)
print(c.area)

Python Class Methods

Instance methods use self. @classmethod receives cls — good for alternate constructors.

@staticmethod needs no self or cls — utility tied to the class namespace.

Real-life example: Instance method is one student raising a hand; classmethod is the whole class taking attendance.

classmethod and staticmethod
class User:
    def __init__(self, name):
        self.name = name

    @classmethod
    def guest(cls):
        return cls("Guest")

    @staticmethod
    def validate(name):
        return len(name) >= 2

print(User.guest().name)
print(User.validate("Al"))

Python Inheritance

A child class extends a parent: class Dog(Animal). It inherits attributes and methods.

Override methods in the child for specialized behavior. super() calls the parent version.

Real-life example: Inheritance is a family recipe — you keep the base dish and add your own spice.

Parent and child
class Animal:
    def speak(self):
        return "..."

class Dog(Animal):
    def speak(self):
        return "Woof!"

print(Dog().speak())