Patterns for clearer python Flashcards
What is an assertation?
At its core, Python’s assert statement is a debugging aid,that tests a condition. If the assert condition is true, nothing happens, and your program continues to execute as normal. But if the condition evaluates to false, an Assertion-error exception is raised with an optional error message.
Why instead of assertions, we may use if-conditions>
Assertions are used to inform unrecoverable errors, where no corrective actions can happen.
Whats the biggest caveat with assertion?
The biggest caveat with assertions is that they can be globally disabled with -o and -OO option
Assertions provoke security holes. How to prevent this/
Never use it for data validation purposes.
Name another caveat of assertions.
Assertions that never fail, specially when it is passed as tuple . assert (x>0,”Message”). Non empty tuples in python always are considered true.
Why opening files with “with” statement is recommended?
It is recommended because it ensures that the file descriptors are closed after the exit of ‘with’
How “with” functionality can be utilized in custom-made classes?
“With” functionality in custom made classes can be implemented with the use of the so call context managers.
What is a context manager?
A context manager is a simple interface/protocol that the custom made classes should follow in order to support “with” functionality/ SImply the classes should contain the __enter__ and __exit__ methods.
What does the leading underscore mean in variable nomenclature?
It is a convention that the variable is used only for internal use.
When the leading underscore violated the convention.
It violates the convention when importing modules. So if you use >>from mymodule import * instead of a normal import >>import mymodule
When trailing underscore is used?
The trailing underscore is used when the most fitting name for a variable is taken.
What a leading double underscore prefix means?
The leading double underscore provokes name mangling.
What is name mangling?
The name mangling is the process that the interpret changes the name of a class attribute to avoid collisions when the class is extended to sub classes.
How the name magling takes place?
Let's say we have a class Test: class Test: def \_\_init\_\_(self,x,u): self._x=x self.\_\_u=u def return_var(self): print(self._x)
class SubTest(Test): def \_\_init\_\_(self,x,y): super().\_\_init\_\_(x,y) self.x="overiden" self.\_\_u='overiden'
print(test.__u) will throw an error
How the name magling takes place?
For the class Test class Test: def \_\_init\_\_(self,x,u): self._x=x self.\_\_u=u def return_var(self): print(self._x)
The dir(Test) lies the following:
[‘_Test__u’, ‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__weakref__’, ‘_x’, ‘return_var’]