def __init__(self, num):
self.lst = []
if num == 0:
self.lst.append(0)
elif num == 1:
self.lst.append(0)
self.lst.append(1)
self.lst = []
if num == 0:
self.lst.append(0)
elif num == 1:
self.lst.append(0)
self.lst.append(1)
2017-12-06
###
else:
self.lst.append(0)
self.lst.append(1)
for j in range(2, num):
self.lst.append(self.lst[j-1] + self.lst[j-2])
else:
self.lst.append(0)
self.lst.append(1)
for j in range(2, num):
self.lst.append(self.lst[j-1] + self.lst[j-2])
2017-12-06
###
def __str__(self):
i = 0
s = '[{0}'.format(self.lst[i])
i += 1
while i < len(self.lst):
s += ', {0}'.format(self.lst[i])
i += 1
s += ']'
return s
def __str__(self):
i = 0
s = '[{0}'.format(self.lst[i])
i += 1
while i < len(self.lst):
s += ', {0}'.format(self.lst[i])
i += 1
s += ']'
return s
2017-12-06
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k, w in kw.iteritems():
setattr(self, k, w)
# http://blog.csdn.net/ly_ysys629/article/details/54893185
self.name = name
self.gender = gender
for k, w in kw.iteritems():
setattr(self, k, w)
# http://blog.csdn.net/ly_ysys629/article/details/54893185
2017-12-06
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
2017-12-06
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
#不禁想到了Java的静态成员变量。。
count = 0
def __init__(self, name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
#不禁想到了Java的静态成员变量。。
2017-12-06
s = 'am I an unicode?'
print isinstance(s, unicode)
print isinstacne(s, str)
r = b'am I an unicode?'
print isinstance(r, unicode)
print isinstacne(r, str)
print isinstance(s, unicode)
print isinstacne(s, str)
r = b'am I an unicode?'
print isinstance(r, unicode)
print isinstacne(r, str)
2017-12-06
try:
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
2017-12-06
from os.path import isdir, isfile
print isdir(r'/data/webroot')
print isfile(r'/data/webroot/conf')
print isdir(r'/data/webroot')
print isfile(r'/data/webroot/conf')
2017-12-06
import functools
sorted_ignore_case = functools.partial(sorted, key = str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
# http://www.wklken.me/posts/2013/08/18/python-extra-functools.html
# https://www.cnblogs.com/kellyseeme/p/5525052.html
sorted_ignore_case = functools.partial(sorted, key = str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
# http://www.wklken.me/posts/2013/08/18/python-extra-functools.html
# https://www.cnblogs.com/kellyseeme/p/5525052.html
2017-12-06