What are the three characteristics of every object? - Answers Every object has
1) an identity
2) a type (its class)
3) a value
What is an object's identity? - Answers A number representing the object's physical location in
memory
What is an object's type? - Answers It's internal data representation and the methods it supports
What happens during a context manager's scope? - Answers If there are no exceptions being
handled, the exit function will receive None as the value of all three of its arguments.
If there are exceptions, the exit function should define what steps, if any, to take, including
automatic resource management (cleanup).
At the end of exit, return either
- True: any pending exception is cleared and the program continues execution normally with the
first statement after the with block
- False: preferrable; if an exception happened, it was not completely handled and should be
propagated up towards the interpreters at runtime
What three protocols should you adhere to rigorously? - Answers 1) Create a proper object
representation using the __repr__() method
2) Iteration is a very big deal; make sure your code works with Python's for statement.
3) Use context managers and the with statement for resource management.
How do you define a function? - Answers - Use the def statement; i.e. def function_name()
How do you check for default variable values and None? - Answers - Default parameters are
evaluated once when the function is first defined, not each time the function is called.
- Use None as the default value for all arguments that should be mutable.
- As a rule, only use immutable objects for default argument values.
When can you mix positional and keyword arguments in a function call? - Answers 1) Positional
arguments appear first in the call.
2) Values are provided for all non-optional arguments.
, 3) No argument receives more than one value.
How do you define a function that accepts all arguments? - Answers def func(*args, **kwargs)
Can you redefine Python precedence evaluation? - Answers No
Write a function to perform math on an arbitrary number of arguments, including proper type
hinting and documentation. - Answers def addMany(first: int, *args) -> int:
if not isinstance(int, first):
raise TypeError("First value must be an integer")
total: int = first
for arg in args:
if isinstance(int, arg):
total += arg
return total
Write code to manipulate a data structure and provide output as requested. - Answers portfolio
= []
portfolio.append(("GOOG", 100, 490.10))
portfolio.append(("IBM", 50, 91.10))
total = sum([shares * price for _, shares, price in portfolio])
Match the following use cases to their Python control structure/keyword(s)
1) Loop infinitely until some condition is ment
2) Iterate over all elements of a sequence
3) Automatically release a resource when it goes out of scope
4) Conditionally execute one of several code blocks
5) Attempt to do something and handle any unusual conditions - Answers 1) while
2) for
3) with
4) if-elif-else