Few Basic ideas of CORE Features in Python

Protocol Based Data Model: -

· The protocol model of python

· The built-in inheritance protocols

· Some caviars around how object orientation works

Few protocols that comes in real handy when we use object orientation (aka Magic Methods/Dunder(double underscored methods): -

__intit__

__repr__

__add__

__len__

__call__

__ and so on …__

Although the use of custom made dunders is discouraged so learning their proper use cases is necessary. Unless you want to actually override their functionality and have the proper idea of implementing them as such so that the whole development team is profited by it, I would suggest otherwise. Cases like devs being ignorant or unaware of the changes or even in a flow of coding, its possible that the methods are used for their trivial functionality but the results come as per the versioned custom method which then leads to failure of test cases. Robustness is not guaranteed so identifying these errors are a huge hackle.

__name__ == ‘__main__’: -

__name__ == ‘__main__’

checks out then it means that this script was run all by itself and wasn’t imported.

#1st Page
if __name__ == ‘__main__’:
print "Run Directly"
else:
print "Run From Import
#2nd
import first module
print "Second Module's Name: {}".format(__name__)

If it was then the code outside that condition will run and whatever is inside it’s scope will not be bothered. Running below codes: 1st page — Run Directly 2nd page — Run From Import Second Module’s Name: main ()

Metaclasses: -

The class creation process itself can be customized by passing the metaclass keyword argument in the class definition line, or by inheriting from an existing class that included such an argument.

Ok so we have a built-in module in python called __build_class__. Well using this option to make sure how the class creation works isn’t really the go-to option. So, the option that we should choose is mentioned below

Every class in python has a metaclass that they are subclasses of. This metaclass class denotes how our class will be structured i.e. its not interface, instead if we want something checked if that exist in the class or subclass, we can use metaclass. If we want a customization, we make our own metaclass and make our own class its Subclass. And the metaclass that we are going to write is a subclass right under Type. This “Type” class is the default metaclass for every class out there in python. So, edit the metaclass by making it Type’s subclass!

@property: -

class Property:def __init__(self, var):## initializing the attributeself.a = var@propertydef a(self):return self.__a## the attribute name and the method name must be same which is used to set the value for the attribute@a.setterdef a(self, var):if var > 0 and var % 2 == 0:self.__a = varelse:self.__a = 2## creating an object for the class 'Property'obj = Property(23)print(obj.a)

There are a few more topics that I would love to mention like: generators, decorators, closures, generator expressions etc. Even though they are pretty common implementing in library code and making your custom features is the tricky part. Hopefully I will mention and put some insight on them in my upcoming week’s post. Surely enough the most excited part is my take on metaclasses and context managers will be something I’ll be working on for my next post.

I hope to never stop Learning!