Skip to content
క
kagitha
/
quiz
/
python-classes
Subscribe
Index
⌘K
JAVA NOTEBOOK · QUIZ 19 · BEGINNER
Python classes, dataclasses and Pydantic
QUESTION 01 / 04
What is the Python equivalent of a Java constructor?
A
A method with the same name as the class
B
__init__
C
__new__ only
D
@constructor
SCORE SO FAR · 0 / 0
Share this quiz ↗
What is the Python equivalent of a Java constructor?
— __init__. Python calls __init__(self, ...) to set up each new object. You create the object by calling the class, without new.
What is self in a Python method?
— The object the method was called on, like Java's this. self is this, but Python makes it an explicit first parameter. Calling doc.length() passes doc as self.
Which Python feature is closest to a Java record?
— @dataclass(frozen=True). A frozen dataclass generates the constructor, __repr__ and __eq__ from its fields and makes the object immutable, like a record.
With class Ticket(BaseModel): priority: int, what does Ticket(priority="2") do?
— Converts it and stores the int 2. Pydantic converts compatible values to the declared type. "2" becomes 2, while "high" would raise a ValidationError.
kagitha
Close
✕
Search everything — articles, videos, chapters, agents, books
01
AI
02
Java
03
System Design
04
Series
05
Manga
06
The Lab
07
Videos
08
Quizzes
09
Books
10
Resume