Python: OOP
__mod__ for
%
__and__ for
&
Define a decorator that would be used to add a setter to the property egg.
@egg.setter
Fill in the blanks to make a setter for the property name.
@name.setter def name(self, value): self._name= value
What is the purpose of prefacing a method name with a single underscore?
To mark it as private, but it doesn't prevent it to be called by external code
__xor__ for
^
What is the magic method for creating an instance?
__init__ (initiate)
functional
using pure functions, higher-order functions, and recursion
two paradigms of programming: imperative
using statements, loops, and functions as subroutines
x + y is translated into
x.__add__(y)
If x hasn't implemented __add__, and x and y are of different types, then
y.__radd__(x) is called (r - reverse)
__or__ for
|
What is A() ^ B() evaluated as, if A doesn't implement any magic methods?
B().__rxor__(A())
The __init__
Method used to initialize an object.
__mul__ for
*
__pow__ for
**
__sub__ for
-
__truediv__ for
/
__floordiv__ for
//
Trying to access an attribute of an instance that isn't defined causes an
AttributeError
What is the difference between a class method and a static method?
Class methods are passed the calling class, static methods aren't
Fill in the blanks to make the egg attribute strongly private and access it from outside of the class.
class Test:/ __egg = 7/t=Test/ t._Test__egg
What is the automatic process by which unnecessary objects are deleted to free memory?
Garbage collection
object-oriented programming (OOP)
Programming concept based on objects and data and how they relate to one another, instead of logic and actions; C++ and Java are OOP languages.
How is a property created?
Using the property decorator
How would the attribute __a of the class b be accessed from outside the class?
_b__a
A static method is one called without ________
an object
Static methods
are similar to class methods, except they don't receive any additional arguments; they are identical to normal functions that belong to a class. They are marked with the @staticmethod decorator.
Which of these is most likely to be a static method?
def spam(x, y): static methods doesn't need additional arguments. so it does not need self or cls
Class
describes what the object will be, but is separate from the object itself.
Strongly private methods and attributes have a
double underscore at the beginning of their names. (__egg)
Static methods behave like plain functions, except
for the fact that you can call them from an instance of the class.
What type of object is a method?
function
The getter
gets the value.
encapsulation
involves packaging of related variables and functions into a single easy-to-use object - an instance of a class.
one common use of magic methods
is operator overloading. This means defining operators for custom classes that allow operators such as + and * to be used on them.
One common use of a property is to
make an attribute read-only
In Python there are no ways
of enforcing a method or attribute be strictly private
Properties
provide a way of customizing access to instance attributes.
All methods must have ________ as their first parameter
self
What are the usual parameter names for the calling instance and the calling class?
self and cls
The setter function
sets the corresponding property's value.
Properties can also be set by defining
setter/getter functions.
magic methods
they are used to create functionality that can't be represented as a normal method.