Skip to content

Instantly share code, notes, and snippets.

View dketov's full-sized avatar

Dmitry Ketov dketov

  • LG Russia R&D Lab
  • St.Petersburg, Russia
View GitHub Profile
@dketov
dketov / 3.3.1.py
Created December 13, 2011 14:21
Передача аргументов
# -*- encoding: utf-8 -*-
"""
Формальные и фактические параметры
"""
def try_to_change(n):
n = 'Changed'
name = 'Init'
try_to_change(name)
@dketov
dketov / 3.4.1.py
Created December 13, 2011 14:23
Возвращаемые значения
# -*- encoding: utf-8 -*-
"""
Возвращение единственного параметра
"""
def inc(x):
return x + 1
foo = 10
foo = inc(foo)
@dketov
dketov / 3.5.1.py
Created December 13, 2011 14:25
Область видимости
# -*- 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
@dketov
dketov / 3.6.1.py
Created December 13, 2011 14:28
Вложенные определения функций
# -*- encoding: utf-8 -*-
"""
Вложенные определения функций
"""
def f1():
x = 88
def f2(x=x):
print x
f2()
@dketov
dketov / 3.7.1.py
Created December 13, 2011 14:33
Встроенные функции
# -*- encoding: utf-8 -*-
"""
Применение функции к последовательности
"""
def cube(x): return x*x*x
print map(cube, range(1, 11))
seq = range(8)
@dketov
dketov / 3.8.1.py
Created December 13, 2011 14:35
Определение классов
# -*- encoding: utf-8 -*-
"""
Простейший класс
"""
class MyClass:
"A simple example class"
i = 12345
def f(self):
return 'hello world'
@dketov
dketov / 3.9.1.py
Created December 13, 2011 14:53
Методы классов
# -*- encoding: utf-8 -*-
"""
Метод, определённый внутри класса
"""
class NextClass: # define class
def printer(self, text): # define method
self.message = text # change instance
print self.message # access instance
@dketov
dketov / 3.10.1.py
Created December 13, 2011 15:24
Атрибуты классов
# -*- encoding: utf-8 -*-
"""
Атрибуты класса
"""
class Bag:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
@dketov
dketov / 3.11.1.py
Created December 14, 2011 05:15
Наследование
# -*- 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())
@dketov
dketov / 3.11.py
Created December 14, 2011 05:22
Перегрузка операторов
# -*- encoding: utf-8 -*-
"""
Перегрузка операторов
"""
class defaultdict(dict):
def __init__(self, default=None):
dict.__init__(self)
self.default = default