In [1]:
class MyOwnClass(object):
"""
This is a documentation for my class, so anyone can read it.
"""
CLASS_CONST = 42
def __init__(self, argument1, default_argument2=1):
"""
This is a constructor of MyOwnClass. It saves all arguments
as a 'protected' variables of a class.
"""
self._argument1 = argument1
self._argument2 = default_argument2
super(MyOwnClass, self).__init__()
def get_info(self):
return ("Class const = %d\nArgument1 = %s\nArgument2 = %d"
% (self.CLASS_CONST, self.argument1, self.argument2))
def print_all(self):
print(self.get_info())
@property
def argument1(self):
return self._argument1
@property
def argument2(self):
return 2**self._argument2
In [2]:
instance_of_my_own_class_1 = MyOwnClass('string arg #1')
instance_of_my_own_class_1.print_all()
In [3]:
instance_of_my_own_class_2 = MyOwnClass('string arg #1', 2)
instance_of_my_own_class_2.print_all()
In [4]:
instance_of_my_own_class_3 = MyOwnClass('string arg #1', default_argument2=4)
instance_of_my_own_class_3.print_all()
In [5]:
instance_of_my_own_class_4 = MyOwnClass(default_argument2=5, argument1=1)
instance_of_my_own_class_4.print_all()
In [6]:
instance_of_my_own_class_5 = MyOwnClass('string arg #1', 'string')
instance_of_my_own_class_5.print_all()
In [7]:
dir(instance_of_my_own_class_1)
Out[7]:
In [8]:
instance_of_my_own_class_1.__dict__
Out[8]:
In [9]:
[i*10 for i in range(10)]
Out[9]:
In [10]:
(i*10 for i in range(10))
Out[10]:
In [11]:
list((i*10 for i in range(10)))
Out[11]:
In [12]:
{i / 2 for i in range(10)}
Out[12]:
In [13]:
set([1,2,3,1,2,3])
Out[13]:
In [14]:
{i: (i - 2, i**2) for i in range(10)}
Out[14]:
In [15]:
dict((i, i**i) for i in range(10))
Out[15]:
In [16]:
map(lambda x: x*10, range(10))
Out[16]:
In [17]:
from functools import reduce
reduce(lambda prev_result, item: prev_result * item, range(1, 5))
Out[17]:
In [18]:
{i: i**2 for i in range(10) if i % 3 == 0}
Out[18]:
In [19]:
[(a, b) for a in range(4) for b in range(0, -a, -1)]
Out[19]:
In [20]:
[1] * 10
Out[20]:
In [21]:
[[1] * 5] * 5
Out[21]:
In [22]:
matrix = [[1] * 5] * 5
matrix
Out[22]:
In [23]:
matrix[3][2] = 2
In [24]:
matrix
Out[24]:
In [25]:
matrix = [[1] * 5 for i in range(5)]
matrix
Out[25]:
In [26]:
matrix[3][2] = 2
In [27]:
matrix
Out[27]:
In [28]:
DEFAULT_LOGIN = 'root'
def my_function(arg1, arg2, arg3='str', arg4=None):
if arg4 is None:
arg4 = {}
if 'login' not in arg4:
arg4['login'] = DEFAULT_LOGIN
if 'id' not in arg4:
arg4['id'] = 0
else:
arg4['id'] += 1
print(arg1, arg2, arg3, arg4)
In [29]:
my_function(1, 2)
In [30]:
args = [10, 20]
kwargs = {
'arg3': 'unicode',
'arg4': {'login': 'frol', 'password': '$(KE)@D)$!1'},
}
In [31]:
my_function(*args, **kwargs)
In [32]:
my_function(*args, **kwargs)
In [33]:
kwargs
Out[33]:
In [34]:
kwargs = {
'arg1': 11,
'arg2': 22,
'arg3': 'unicode',
'arg4': {'login': 'frol', 'password': '$(KE)@D)$!1'},
}
In [35]:
my_function(**kwargs)
In [36]:
kwargs
Out[36]:
In [37]:
def my_function2(*args, **kwargs):
login = DEFAULT_LOGIN
if 'arg4' in kwargs and 'login' in kwargs['arg4']:
login = kwargs['arg4']['login']
kwargs['arg3'] = login
my_function(*args, **kwargs)
In [38]:
my_function2(**kwargs)