1.4.2 · HinglishPython & Scientific Computing

Functions, classes, and modules

3,572 words16 min readRead in English

1.4.2 · AI-ML › Python & Scientific Computing

Functions: Reusable Logic Blocks

ML/AI Mein Functions Kyun Zaroori Hain

AI/ML workflows mein, tum baar baar yeh karte ho:

  • Data preprocess karna (normalize, tokenize, augment)
  • Metrics compute karna (accuracy, F1, loss)
  • Models ko different hyperparameters ke saath train karna

Functions ke bina: Code ko har jagah copy-paste karo, bugs multiply hoti hain, changes karne ke liye 50 jagah edit karna padta hai.

Functions ke saath: Ek baar likho, baar baar call karo, ek hi jagah bugs fix karo.

Figure — Functions, classes, and modules

Classes: Data + Behavior Bundles

Classes ko First Principles Se Derive Karna

Modules: Code ko Files Across Organize Karna

Modules Kyun? Scale Se Derivation

Advanced Concepts: *args, **kwargs, Decorators

Variable-Length Arguments

Recall Ek 12-saal ke bachche ko explain karo

Socho tum apna school backpack organize kar rahe ho: Functions labeled pouches ki tarah hain. Tumhare paas ek "pencil pouch" (function) hai jo saari pencils rakhta hai. Pencils ko loose har jagah dump karne ki jagah, tum unhe pouch mein rakhte ho. Jab pencil chahiye, tum exactly jaante ho kahan dekhna hai. Agar tuti pencils replace karni hain, tum sirf pouch fix karte ho, poora backpack search nahi karte.

Classes tumhare calculator ki tarah hain. Yeh sirf buttons (functions) nahi hai – yeh numbers bhi yaad rakhta hai jo tumne type kiye (data/state). Ek "Calculator" class mein memory slots (attributes) aur buttons (methods) hote hain. Har student ka apna calculator hai apne stored numbers ke saath, lekin sabhi calculators same tarike se kaam karte hain.

Modules alag textbooks ki tarah hain. Tum apni saari books har jagah nahi le jaate – math class mein math textbook le jaate ho, science class mein science textbook. Har book (module) mein related cheezein hoti hain. Tumhara homework script in books se "import" karta hai: from math_textbook import quadratic_formula.

AI banate waqt, tumhare paas ek data_cleaning.py module (textbook) ho sakta hai jisme remove_duplicates() function (pouch) aur ek NeuralNetwork class (fancy calculator jo weights yaad rakhta hai aur predictions compute kar sakta hai) hogi.

Doosre Topics Se Connections

  • Python basics - syntax, data types, control flow - Functions internally control flow use karte hain
  • NumPy arrays and vectorization - Classes aksar NumPy arrays ko attributes ke roop mein wrap karti hain
  • ML mein Object-oriented programming - Inheritance, polymorphism ka deep dive ML examples ke saath
  • scikit-learn API patterns - sklearn ka fit/predict pattern class-based design hai
  • Neural networks scratch se banana - Layer abstractions ke liye classes zaroori hain
  • Data pipelines aur preprocessing - Modules pipeline stages organize karte hain
  • ML ke liye Software engineering - Modules ki testing, documentation, version control

#flashcards/ai-ml

Python mein teen fundamental code organization units kya hain? :: Functions (reusable logic), classes (data + behavior), aur modules (related code organize karne wali files).

Python class mein __init__ method ka purpose kya hai?
Constructor method jo object create karte waqt instance attributes initialize karta hai. Automatically call hota hai jab tum obj = ClassName(args) likhte ho.

Function definitions mein mutable default arguments kyun avoid karne chahiye? :: Mutable defaults (jaise [] ya {}) function definition ke time par ek baar create hote hain aur calls ke across persist karte hain, unexpected shared state cause karte hain. None use karo aur function ke andar naya object banao.

Function signature mein *args kya allow karta hai?
Variable number of positional arguments ko tuple ke roop mein accept karta hai. Flexible functions enable karta hai jaise create_model(100, 50, 10) kisi bhi number of layers ke saath.
from module import function aur import module mein kya difference hai?
Pehla specific names ko current namespace mein import karta hai (function()), doosra module ko itself import karta hai jisme prefix chahiye (module.function()). Pehla convenient hai, doosra name conflicts avoid karta hai.
Class methods ko self pehle parameter ke roop mein kyun chahiye?
self us instance ko refer karta hai jo method call kar raha hai. Jab tum obj.method(x) call karte ho, Python internally Class.method(obj, x) karta hai, instance explicitly pass karta hai.
Module mein if __name__ == '__main__': kya karta hai?
Andar ka code sirf tab run hota hai jab file directly execute ki jaaye (python module.py), import karne par nahi. Testing aur demos ke liye use hota hai imports affect kiye bina.
KNN classifier class mein fit() sirf data kyun store karta hai?
KNN ek lazy learner hai – koi training computation nahi hoti. Yeh training data memorize karta hai aur prediction ke time saara kaam karta hai (stored samples tak distances compute karke).
Classes jo Scikit-learn API pattern follow karta hai kya hai?
Estimators ke paas training ke liye fit(X, y) aur inference ke liye predict(X) hota hai. Yeh convention different models ke across consistent interface enable karta hai.
fit() method se self kyun return karo?
Method chaining enable karta hai jaise Model().fit(X, y).predict(X_test), sklearn API design ka ek convenient pattern hai.

Concept Map

unit of

unit of

unit of

provides

bundles

groups

takes

optionally gives

documented by

applied in

example uses

Code Organization

Functions

Classes

Modules

Reusable Logic

Data plus Behavior

Related Code in Files

Parameters

Return Value

Docstring

ML Workflows