class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
#self.__dict__.update(kw)
#for k,v in kw.iteritems():
# setattr(self,k,v)
for i in kw:
setattr(self,i,kw[i])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
#self.__dict__.update(kw)
#for k,v in kw.iteritems():
# setattr(self,k,v)
for i in kw:
setattr(self,i,kw[i])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2017-11-23
t = (t2 - t1) * 1000 if unit=='ms' else (t2 - t1)相当于
if unit =='ms':
t=(t2-t1)*1000
else:
t=t2-t1
print 'call %s() in %f %s'%(f.__name__,t,unit)
if unit =='ms':
t=(t2-t1)*1000
else:
t=t2-t1
print 'call %s() in %f %s'%(f.__name__,t,unit)
2017-11-23
def performance(unit):
def fn(f):
def ft(x):
a = time.time()
f(x)
b = time.time()
print 'call %s() in %s %s'%(f.__name__,(b-a),unit)
return f(x)
return ft
return fn
def fn(f):
def ft(x):
a = time.time()
f(x)
b = time.time()
print 'call %s() in %s %s'%(f.__name__,(b-a),unit)
return f(x)
return ft
return fn
2017-11-23
import time
def performance(unit):
def fn(f):
def ft(x):
a = time.time()
f(x)
b = time.time()
print 'call %s() in %s %s'%(f.__name__,(b-a),unit)
return f(x)
return ft
return fn
def performance(unit):
def fn(f):
def ft(x):
a = time.time()
f(x)
b = time.time()
print 'call %s() in %s %s'%(f.__name__,(b-a),unit)
return f(x)
return ft
return fn
2017-11-23
import math
def is_sqr(x):
return math.sqrt(x) in range(1,11)
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x) in range(1,11)
print filter(is_sqr, range(1, 101))
2017-11-23
class Person(object):
def __init__(self, name, gender, birth, **kwargs):
self.name = name
self.gender = gender
self.birth = birth
for key,value in kwargs.items():
setattr(self,key,value)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
def __init__(self, name, gender, birth, **kwargs):
self.name = name
self.gender = gender
self.birth = birth
for key,value in kwargs.items():
setattr(self,key,value)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
2017-11-22
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2017-11-22
class Person(object):
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print(Person.__count)
p1 = Person('Bob')
p2 = Person('Alice')
try:
print(Person.__count)
except AttributeError:
print("AttributeError")
python3.x
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print(Person.__count)
p1 = Person('Bob')
p2 = Person('Alice')
try:
print(Person.__count)
except AttributeError:
print("AttributeError")
python3.x
2017-11-22
class Persons(object):
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
for k, v in kw.items():
setattr(self,k,v)
xiaoming = Persons('Xiao Ming', 'Male', '1990-1-1', job='Student')
python3.x
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
for k, v in kw.items():
setattr(self,k,v)
xiaoming = Persons('Xiao Ming', 'Male', '1990-1-1', job='Student')
python3.x
2017-11-22