A class defines behavior and data; an instance is a runtime object created by calling the class. If you know Java or C# classes, Python OOP feels familiar—but everything is public by convention and self is explicit.
Defining and using a class
class User:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hi, {self.name}!"
ada = User("Ada")
print(ada.greet())
__init__ initializes instances. Methods take self as the first parameter—the instance reference.
Instance vs class attributes
Attributes on self are per-instance; attributes on the class are shared—mutable class attributes can surprise you if used as instance defaults.
Important interview questions and answers
- Q: What is self?
A: Reference to the current instance passed explicitly—unlike Java's implicitthis. - Q: __init__ vs __new__?
A:__new__creates the object;__init__initializes it—rarely override__new__except for immutables or singletons.
Self-check
- What method initializes a new instance?
- Why is self required on instance methods?
Tip: Explicit self on methods—unlike implicit this in Java.
Interview prep
- What is self?
Explicit instance reference as first parameter—caller does not pass it; Python binds automatically.
- __init__ vs __new__?
__new__creates instance;__init__initializes—rarely override __new__ except immutables.