মূল content-এ যাও

Python for AI/Object-Oriented Programming/Lesson 23

Methods ও Attributes

Instance বনাম class attribute, @property দিয়ে হিসাব করা attribute, @classmethod দিয়ে বিকল্প constructor, আর @staticmethod — কোনটা কখন।

সময়
22 মিনিট
Exercise
1
Challenge
1
Quiz
3 প্রশ্ন

সহজ ভাষায়

আগের lesson-এ প্রতিটা object-এর নিজের data দেখেছি। আজ চার ধরনের জিনিস:

কীকোথায় থাকেউদাহরণ
Instance attributeপ্রতিটা object-এ আলাদাself.name
Class attributeClass-এ, সব object শেয়ার করেcount = 0
@propertyদেখতে attribute, আসলে হিসাবrect.area
@classmethod / @staticmethodObject ছাড়াই ডাকা যায়Temp.from_fahrenheit(98)

কেন দরকার?

  • Class attribute — সব object-এর জন্য একই মান: default setting, constant, counter।
  • Property — data সবসময় সঠিক রাখা। area আলাদা করে রাখলে width বদলানোর পর ভুল থেকে যেতে পারে; property প্রতিবার নতুন করে হিসাব করে।
  • Classmethod — একই জিনিস বানানোর বিকল্প উপায়। Hugging Face-এর বিখ্যাত AutoModel.from_pretrained("bert-base-uncased") একটা classmethod!

Instance বনাম Class attribute

main.py

university আর total class-এ থাকে — a বা b-তে খুঁজলে না পেয়ে Python class-এ খোঁজে।

সাবধান: object থেকে class attribute "বদলানো"

main.py

a.university = "DU" class-এর মান বদলায়নি — a-তে নতুন একটা instance attribute বানিয়েছে যেটা class-এরটাকে ঢেকে দিয়েছে। Class attribute বদলাতে চাইলে Student.university = ... লেখো।

আর mutable class attribute (যেমন history = []) সাবধান — সব object একই list শেয়ার করবে। প্রতিটা object-এর আলাদা list চাইলে __init__-এ self.history = []।

@property

main.py

r.area — bracket নেই, কিন্তু প্রতিবার নতুন করে হিসাব হয়। Width বদলালে area নিজেই ঠিক থাকে।

Setter আর validation

Property দিয়ে মান বসানোর সময় চেকও করা যায়:

main.py

_lr-এর শুরুতে _ — Python convention: "এটা ভেতরের জিনিস, বাইরে থেকে সরাসরি ছুঁয়ো না।"

@classmethod: বিকল্প constructor

main.py

cls হলো class নিজেই (Student)। cls(...) মানে Student(...) — নতুন object।

@staticmethod: শুধু সম্পর্কিত একটা function

Object বা class কোনোটারই data লাগে না, কিন্তু যুক্তিসঙ্গতভাবে class-এর সাথে যায়:

main.py
প্রথম parameterকখন
সাধারণ methodself (object)Object-এর data লাগে
@classmethodcls (class)নতুন object বানানো, class-level কাজ
@staticmethodকিছুই নাশুধু সম্পর্কিত helper

Exercise

Exercise

Rectangle

+20 XP

Rectangle class বানাও:

  • Rectangle(width, height)
  • area() আর perimeter() method
  • একটা class attribute count — মোট কতগুলো Rectangle বানানো হয়েছে (শুরুতে 0, প্রতিটা নতুন object-এ ১ বাড়বে)
solution.py

Quiz

  1. Q1নিচের code-এর output কী?
    class Dog:
        species = "Canis familiaris"
    
    a = Dog()
    b = Dog()
    Dog.species = "Wolf"
    print(a.species, b.species)
    
  2. Q2@property দিয়ে বানানো area কীভাবে ব্যবহার করবে?
  3. Q3@classmethod-এর প্রথম parameter কী পায়?
0/3 answered

Challenge

Challenge

Temperature — property আর classmethod

+50 XP

Temperature class বানাও যেটা ভেতরে শুধু celsius রাখে:

  • Temperature(celsius)
  • fahrenheit একটা `@property` — t.fahrenheit (bracket ছাড়া) লিখলে হিসাব করে দেবে
  • fahrenheit-এর setter — t.fahrenheit = 212 লিখলে celsius আপডেট হবে
  • Temperature.from_fahrenheit(f) — একটা `@classmethod` যেটা Fahrenheit থেকে নতুন object বানায়
solution.py

বাস্তবে কোথায় ব্যবহার হয়?

Hugging Face Transformers-এ model load করা:

from transformers import AutoModel
model = AutoModel.from_pretrained("bert-base-uncased")

from_pretrained একটা classmethod — internet থেকে config আর weights download করে, তারপর cls(...) দিয়ে object বানায়। চলো ছোট একটা সংস্করণ বানাই:

main.py

Class attribute (REGISTRY), classmethod, **config unpacking, property — সব একসাথে।

Interview প্রশ্ন

  • Beginner: Class attribute আর instance attribute-এর পার্থক্য কী?
  • Intermediate: @property কেন ব্যবহার করবে, সরাসরি attribute না রেখে?
  • Advanced: @classmethod আর @staticmethod-এর পার্থক্য কী? Inheritance-এ cls কেন গুরুত্বপূর্ণ? (Subclass থেকে ডাকলে cls হয় subclass — তাই সঠিক type-এর object তৈরি হয়।)

এরপর কী?

PyTorch-এ model লেখা শুরু হয় class MyModel(nn.Module): দিয়ে — অর্থাৎ আরেকটা class থেকে সবকিছু "উত্তরাধিকার" নিয়ে। পরের lesson: Inheritance।