Python Advanced - LogeshVel/learning_resources GitHub Wiki

Awsome Pythontips


Dynamic Attributes

image

image


Still we could change the value by self._x = "somevalue". But it is not intended to do that. Since _var in python means its private, eventhough we can change we should not do that.. thats the reason for that _var.

image

Advanced flow control

while else

image

image

image

for else

The else clause executes after the loop completes normally. This means that the loop did not encounter a break statement.

Same as while else. but in the while else when the condition is false the else is executed and when the loop is terminated by break the else won't execute.

In simple words, under normal condition else clause is executed.

try else

image

image

image

Single dispatch

When the implementation is chosen based on the type of a single argument, this is known as single dispatch.

Single dispatch

image

image

image

bytes

bytes to str, hex

image

image

bytearray

image

image

image

Descriptors

Property decorator

image

image

image

Descriptors

Any object which defines the methods __get__(), __set__(), or __delete__().

Property is a descriptor

property uses the decorator for the same result. But if we have 3 methods(get, set and del) and then give it to the proprty(fget, fset, fel, doc) then its the Descriptors.

descriptors are a low-level mechanism that lets you hook into an object's attributes being accessed. Properties are a high-level application of this; that is, properties are implemented using descriptors. Or, better yet, properties are descriptors that are already provided for you in the standard library.

image

image

descriptor-howto-guide

Implementing Descriptors

image

*Note: Descriptors. We can bind getter, setter (and deleter) functions into a separate class, which denotes the property class

Descriptors - property( fget, fset, fdel, doc)

propperty = @property, @attr.setter,...

image

implementing descriptor

image

ex

image

in the above example we could see that the in the init the values are assigned for the property and in the body of the class the descriptors is created for that property. Positive() class has the get, set and del methods. So here, binding getter, setter (and deleter) functions into a separate class

Also, the body of the class is first executed and then the init will be executed. So binding happens and then init will happen.

ex:

image

backend of descriptor

image

image

Data vs Non-Data descriptors

image

Attributes Lookup precedence

image

image

__new__

__new__ is a static method that returns the object for the class.

image

The Object created by the new method is been used by the init to initialize the values for the create instance.

__new__ vs __init__

new - creation

init - initialization

image

image

Metaclass

not much info i have provided

image

image

image

Abstract Class

image

image

image

image

image

image

image

image

image

image

ABC library helps to create the Abstract class

image

abstract method

image

image

Concrete class

Opposite to the abstract class.. We can create object for the concrete class.