This file contains 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
{ | |
"name": "", | |
"displayName": "", | |
"description": "", | |
"author": "", | |
"website": "", | |
"version": "", | |
"issetuprecipe": false, | |
"categories": [], | |
"tags": [], |
This file contains 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
Action<int> printRoot = delegate(int number) | |
{ | |
Console.WriteLine(Math.Sqrt(number)); | |
}; | |
Action<IList<double>> printMean = delegate(IList<double> numbers) | |
{ | |
double total = 0; |
This file contains 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
# try_except_else_finally 例子,如果没有错误发生,可以在except语句块后面加一个else,当没有错误发生时,会自动执行else语句: | |
try: | |
print('try...') | |
r = 10 / int('2') | |
print('result:', r) | |
except ValueError as e: | |
print('ValueError:', e) | |
except ZeroDivisionError as e: | |
print('ZeroDivisionError:', e) |
This file contains 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
>>> class Student(object): | |
... def __init__(self, name): | |
... self.name = name | |
... def __str__(self): | |
... return 'Student object (name: %s)' % self.name | |
... | |
>>> print(Student('Michael')) | |
Student object (name: Michael) |
This file contains 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
class Student(object): | |
@property | |
def birth(self): | |
return self._birth | |
@birth.setter | |
def birth(self, value): | |
self._birth = value |
This file contains 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
class Student(object): | |
def get_score(self): | |
return self._score | |
def set_score(self, value): | |
if not isinstance(value, int): | |
raise ValueError('score must be an integer!') | |
if value < 0 or value > 100: | |
raise ValueError('score must between 0 ~ 100!') |
This file contains 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
# 使用functools.partial创建新的函数 | |
>>> import functools | |
>>> int2 = functools.partial(int, base=2) | |
>>> int2('1000000') | |
64 | |
>>> int2('1010101') | |
85 |
This file contains 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
>>> sorted([36, 5, -12, 9, -21]) | |
[-21, -12, 5, 9, 36] |
This file contains 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
>>> from functools import reduce | |
>>> def add(x, y): | |
... return x + y | |
... | |
>>> reduce(add, [1, 3, 5, 7, 9]) | |
25 |
This file contains 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
//Iterator是惰性序列,因此通过list()函数让它把整个序列都计算出来并返回一个list | |
# list、dict、str等数据类型不是Iterator ,需要用iter()函数转换一下;可作用于for循环的对象都是Iterable类型;可作用于next()函数的对象都是Iterator类型; |
NewerOlder