1/15
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a function in Python?
A reusable block of code that performs a specific task, defined with the def keyword. It can take arguments and return values.
How do you call a method on an object?
Using dot notation: obj.method_name(arguments).
What is the difference between an attribute and a method?
An attribute is data stored within an object (e.g., obj.name), while a method is a function that belongs to the object (e.g., obj.calculate()).
What does the return statement do in a function?
It exits the function and optionally passes a value back to the caller. If omitted, the function returns None.
How do you check if an object has a specific attribute?
Use hasattr(obj, 'attribute_name'), which returns True or False.
What is a magic method in Python?
A special method with double underscores (e.g., __init__) that defines behavior for built-in operations like initialization or arithmetic.
What is the purpose of __init__?
It's the constructor method called when an object is created, used to initialize its attributes.
What is 'duck typing'?
Determining an object's type by its behavior (methods/attributes) rather than its explicit class. Example: 'If it quacks like a duck, it's a duck.'
How do you dynamically access an attribute using a string name?
Use getattr(obj, 'attribute_name').
What is a ternary expression?
A concise if-else in one line: value = true_expr if condition else false_expr.
What is the self parameter in a method?
A reference to the current instance of the class, automatically passed as the first argument to instance methods.
How do you list all methods/attributes of an object?
Use dir(obj) or tab-completion in IPython/Jupyter.
What is a docstring?
A string literal (enclosed in triple quotes) at the start of a function/class/module that documents its purpose. Accessed via obj.__doc__.
What's the difference between mutable and immutable objects?
Mutable objects (e.g., lists) can be modified after creation; immutable ones (e.g., strings) cannot.
What does pass do?
A no-op placeholder used where syntax requires code but no action is needed (e.g., empty functions).
How do you handle variable-length arguments in a function?
Use args for positional arguments and *kwargs for keyword arguments.