This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Формальные и фактические параметры | |
""" | |
def try_to_change(n): | |
n = 'Changed' | |
name = 'Init' | |
try_to_change(name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Возвращение единственного параметра | |
""" | |
def inc(x): | |
return x + 1 | |
foo = 10 | |
foo = inc(foo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Глобальные и локальные переменные | |
""" | |
# global scope | |
X = 99 # X and func assigned in module: global | |
def func(Y): # Y and Z assigned in function: locals | |
# local scope |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Вложенные определения функций | |
""" | |
def f1(): | |
x = 88 | |
def f2(x=x): | |
print x | |
f2() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Применение функции к последовательности | |
""" | |
def cube(x): return x*x*x | |
print map(cube, range(1, 11)) | |
seq = range(8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Простейший класс | |
""" | |
class MyClass: | |
"A simple example class" | |
i = 12345 | |
def f(self): | |
return 'hello world' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Метод, определённый внутри класса | |
""" | |
class NextClass: # define class | |
def printer(self, text): # define method | |
self.message = text # change instance | |
print self.message # access instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Атрибуты класса | |
""" | |
class Bag: | |
def __init__(self): | |
self.data = [] | |
def add(self, x): | |
self.data.append(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Наследование | |
""" | |
def rotate_tires( car ) : | |
for i in range(1, car.tire_count()) : | |
print "Moving tire from " + str(i) | |
car.set_tire( i+1 ) | |
print "Moving tire from " + str(car.tire_count()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Перегрузка операторов | |
""" | |
class defaultdict(dict): | |
def __init__(self, default=None): | |
dict.__init__(self) | |
self.default = default |