Hello, here we are to discuss some amazing features of python which are making python magical.
1. Getting Code of Python Object
In python, we can get the internal implementation of the python object by using inspect
module. It is used to introspect live objects in a program and look at the source code of modules, classes, and functions that are used throughout a program. This is powerful because this module can actually be used to extract the source code of a function itself, parse the arguments which that function accepts, and related library documentation.
For example:
For getting code of complete module:
For getting code of a class or method:
2. exec()
function in Python
exec()
function is used for the dynamic execution of Python program which can either be a string or object code. If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed. We must be careful that the return statements may not be used outside of function definitions not even within the context of code passed to the exec()
function. It doesn't returns any value, hence returns None
.
Syntex of exec()
:
exec(object[, globals[, locals]])
Parameters:
- object :- this can be a string or object code
- globals :-This can be a dictionary and the parameter is optional
- locals :-This can be a mapping object and is also optional
The
exec()
function accepts large blocks of code, unlike theeval()
function which only accepts a single expression
Example:
x = 'name = "John"\nprint(name)'
exec(x)
Output:
John
3.__call__
methode in Python
Python has a set of built-in methods and __call__
is one of them. The __call__
method enables Python programmers to write classes where the instances behave like functions and can be called like a function. This means we can call a class object as a function. That is an amazing benefit of python and feels like magic. There is too much confusion in function, class, and instances so see this using example.
Example :
Output:
Instance Created
200
4. Metaclass
A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass. A class in Python defines how the instance of the class will behave. In order to understand metaclasses well, one needs to have prior experience working with Python classes. Before we dive deeper into metaclasses, let's get a few concepts out of the way.
Creating Custom Metaclasses
In Python, we can customize the class creation process by passing the metaclass keyword in the class definition. This can also be done by inheriting a class that has already passed in this keyword.
class MyMeta(type):
pass
class MyClass(metaclass=MyMeta):
pass
class MySubclass(MyClass):
pass
We can see below that the type of MyMeta class is type and that the type of MyClass and MySubClass is MyMeta.
Code:
print(type(MyMeta))
print(type(MyClass))
print(type(MySubclass))
Output:
<class 'type'>
<class '__main__.MyMeta'>
<class '__main__.MyMeta'>
"Metaclasses are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why)."
5. Dataclass
One new and exciting feature coming in Python 3.7 is the data class. A data class is a class typically containing mainly data, although there aren’t really any restrictions. It is created using the new @dataclass decorator, as follows:
from dataclasses import dataclass
@dataclass
class DataClassCard:
rank: str
suit: str
A data class comes with basic functionality already implemented. For instance, you can instantiate, print, and compare data class instances straight out of the box:
>>> queen_of_hearts = DataClassCard('Q', 'Hearts')
>>> queen_of_hearts.rank
Output:
'Q'
Thanks for reading
Top comments (0)