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-02-27
class Person(object):
count = 0
def __init__(self,name):
self.name = name
Person.count += 1
count = 0
def __init__(self,name):
self.name = name
Person.count += 1
2018-02-27
class Person(object):
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
self.other = kw
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.other['job']
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
self.other = kw
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.other['job']
2018-02-27
#利用伪私有
class Person(object):
__count = 0
def __init__(self, name):
Person.__count=Person.__count+1
self.name=name
print self._Person__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print('AttributeError:')
class Person(object):
__count = 0
def __init__(self, name):
Person.__count=Person.__count+1
self.name=name
print self._Person__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print('AttributeError:')
2018-02-26
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)
self.name = name
self.gender = gender
self.birth = birth
for k,v in kw.items():
setattr(self,k,v)
2018-02-26
def __init__(self,name,gender,birth,job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
self.name = name
self.gender = gender
self.birth = birth
self.job = job
2018-02-26
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'
2018-02-26
import os.path
from os.path import isdir
from os.path import isfile
from os.path import isdir
from os.path import isfile
2018-02-26
sorted_ignore_case = functools.partial(sorted,key = lambda s:s.lower())
2018-02-26
@functools.wraps(f)
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
2018-02-26