Patterns for clearer python Flashcards

1
Q

What is an assertation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why instead of assertions, we may use if-conditions>

A

Assertions are used to inform unrecoverable errors, where no corrective actions can happen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Whats the biggest caveat with assertion?

A

The biggest caveat with assertions is that they can be globally disabled with -o and -OO option

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Assertions provoke security holes. How to prevent this/

A

Never use it for data validation purposes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name another caveat of assertions.

A

Assertions that never fail, specially when it is passed as tuple . assert (x>0,”Message”). Non empty tuples in python always are considered true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why opening files with “with” statement is recommended?

A

It is recommended because it ensures that the file descriptors are closed after the exit of ‘with’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How “with” functionality can be utilized in custom-made classes?

A

“With” functionality in custom made classes can be implemented with the use of the so call context managers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a context manager?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the leading underscore mean in variable nomenclature?

A

It is a convention that the variable is used only for internal use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When the leading underscore violated the convention.

A
It violates the convention when importing modules.
So if you use 
>>from mymodule import *
instead of a normal import
>>import mymodule
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When trailing underscore is used?

A

The trailing underscore is used when the most fitting name for a variable is taken.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What a leading double underscore prefix means?

A

The leading double underscore provokes name mangling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is name mangling?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How the name magling takes place?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How the name magling takes place?

A
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’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Does the name mangling(double leading underscore) apply to methods?

A

Yes

17
Q

What is dunder?

A

Double underscores (__init__)

18
Q

How many string formatting methods exits?

A

Four

a) Old way
b) New way
c) Formatted string literals
d) Template

19
Q

Describe old way of string formatting

A

Using % operator,it’s a shortcut that lets you do simple positional formatting very easily.

‘Hello, %s’ % name

20
Q

Describe the new way of formatting

A

Using the format command
»” My name is {name}”.format(name=”Mitsos”)

With this implementation, the position of arguments is negleted

21
Q

Formatted string literals

A

f’ My name is {name}’

This method needs to initialize previously the name variable