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:
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:
print 'AttributeError'
2018-04-02
class Person(object):
count=0
def __init__(self,name):
self.name=name
p1 = Person('Bob')
Person.count=Person.count+1
print Person.count
p2 = Person('Alice')
Person.count=Person.count+1
print Person.count
p3 = Person('Tim')
Person.count=Person.count+1
print Person.count
count=0
def __init__(self,name):
self.name=name
p1 = Person('Bob')
Person.count=Person.count+1
print Person.count
p2 = Person('Alice')
Person.count=Person.count+1
print Person.count
p3 = Person('Tim')
Person.count=Person.count+1
print Person.count
2018-04-02
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except: print 'attributeerror'
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except: print 'attributeerror'
2018-04-02
L1=[p1,p2,p3]
L2=sorted(L1, key=lambda x: x.name)
for item in L2:
print(item.name)
L2=sorted(L1, key=lambda x: x.name)
for item in L2:
print(item.name)
2018-04-02
isinstance(s, unicode) 里的unicode不识别啊?!!!!!!!,一直是红色下划线报错,改成str就好了
2018-04-02
已采纳回答 / 知多少2017
calc_prod([1, 2, 3, 4] 的返回值类似函数的引用,不是函数值,需要后面加上()才能得到最后结果,其实你可以print calc_prod([1, 2, 3, 4])()试试。另外里层函数外面是调用不到的,不存在混淆。
2018-04-01
import math
def is_sqr(x):
if len(str(math.sqrt(x))) < 6 :
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if len(str(math.sqrt(x))) < 6 :
return x
print filter(is_sqr, range(1, 101))
2018-03-31
比较晦涩的东西,老师却能几句话说清楚,多厉害啊,虽然一下还写不出来,但是这是一个过程,积淀之下懂得越来越多
2018-03-31
def cmp_ignore_case(s1, s2):
if s1[0].lower()<s2[0].lower():
return -1
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].lower()<s2[0].lower():
return -1
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-03-31
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})
2018-03-30
class Fib(object):
def __init__(self, num):
self.fib = [0,1]
while len(self.fib) < num:
self.fib.append(self.fib[-2]+self.fib[-1])
def __len__(self):
return len(self.fib)
def __str__(self):
return str(self.fib)
f = Fib(10)
print f
print len(f)
def __init__(self, num):
self.fib = [0,1]
while len(self.fib) < num:
self.fib.append(self.fib[-2]+self.fib[-1])
def __len__(self):
return len(self.fib)
def __str__(self):
return str(self.fib)
f = Fib(10)
print f
print len(f)
2018-03-30