Python is a programming language. Python is often refereed as a scripting language as scripting languages are often interpreted and not compiled.
if __name__ == "__main__":
# Do anything you want
pass
You need this code segment becasue it allows you to execute the code within it when you are directly running the file. So whe nyou create modules you need some code but it must not be executed when you import that module in that case this if statement serves the need.
# If you want to give arguments to a function as a list
def f(x,y):
print(x, y)
myList= [1,2]
f(*myList)
1 2
Simple alternative to metaclasses. The '@' indicates the application of decorator.
@myDecorator
def aFunction():
print('Inside aFunction')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-12-da5cae9cd3fc> in <module>() ----> 1 @myDecorator 2 def aFunction(): 3 print('Inside aFunction') NameError: name 'myDecorator' is not defined
class my_decorator(object):
def __init__(self, f):
print("inside my_decorator.__init__()")
f()
def __call__(self):
print("inside my_decorator.__call__()")
@my_decorator
def aFunction():
print("inside aFunction()")
# The part before it would run before going to the next statement
# as we used decorators in which we defined the __init__ function
print("Finished decorating aFunction()")
aFunction()
# Decorators replace the original function object with the __call__
inside my_decorator.__init__() inside aFunction() Finished decorating aFunction() inside my_decorator.__call__()
How decorators work?
When @my_decorator is called it goes to the __init__ function and executes it. We can also run the original function inside the init as function declaration is complete before executing the __init__. After the init function the original function is decorated that is replaced with the __call__ function so when we run original function the code of __call__ is executed.
def foo():
pass
foo = staticmethod(foo)
# You can replace the above code using decorator as
@staticmethod
def foo():
pass
Objects are created by other objects called classes. To modify classes we use metaclasses. Default metaclass is 'type'. In simple terms metaclasses create classes. In metaclass creation you may see __new__ and __init__. new is used for creation of a new class and init is called after the class is created.
# args and kwargs are convention names you can also write anything like
# *first , **second
# They are used in function definition allowing you to pass variable number of arguments to a function
# *args wraps the arguments
def add(*args):
rv = 0
for val in args:
rv = rv + val
return rv
add(1,2,3)
6
# *args is used to send non-keyworded argument list to a function
# **kwargs allows you to pass kwyworded variable length of arguments
def print_keywords(**kwargs):
if kwargs is not None:
for var_name, var_value in kwargs.items():
print('THe var_name is %s and var_value is %s'%(var_name, var_value))
print_keywords(name='Advanced Python', language='Python', vaersion=3.6)
THe var_name is name and var_value is Advanced Python THe var_name is language and var_value is Python THe var_name is vaersion and var_value is 3.6