class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
p = Person('Bob', 'Male')
setattr(p, 'age', 18)
setattr(p, 'course','Python' )
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
p = Person('Bob', 'Male')
setattr(p, 'age', 18)
setattr(p, 'course','Python' )
print p.age
print p.course
2016-05-07
class Person(object):
__count = 0
@classmethod
def how_many(cls):
print cls.__count
cls.__count += 1
print cls.__count
def __int__(self,name):
self.name = name
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
print cls.__count
cls.__count += 1
print cls.__count
def __int__(self,name):
self.name = name
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2016-05-07
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >80:
print 'A'
elif self.__score >= 60:
print 'B'
else:
print 'C'
self.name = name
self.__score = score
def get_grade(self):
if self.__score >80:
print 'A'
elif self.__score >= 60:
print 'B'
else:
print 'C'
2016-05-06
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2016-05-06
sorted_ignore_case = functools.partial(sorted,key=str.upper)
#或者参数key = str.lower都可以
#或者参数key = str.lower都可以
2016-05-06
def __init__(self, name, score):
self__name=name
self__score=score
if self__score>80:
print 'A-优秀'
elif self__score>60:
print 'B-及格'
else:
print 'C-不及格'
self__name=name
self__score=score
if self__score>80:
print 'A-优秀'
elif self__score>60:
print 'B-及格'
else:
print 'C-不及格'
2016-05-06
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
2016-05-06
这两个是python中的可变参数。*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前
2016-05-06
import time
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
@performance('ms')
.......
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
@performance('ms')
.......
2016-05-06