实例化一个对象的时候, 实例化对象引用了类的属性(能够访问).
修改类的属性, 实例化对象访问/引用的属性值相应改变.
但是如果修改实例的属性值, 将会引用到一个新的内存地址, 而这个实例的被修改属性将不再引用类的属性值.
修改类的属性, 实例化对象访问/引用的属性值相应改变.
但是如果修改实例的属性值, 将会引用到一个新的内存地址, 而这个实例的被修改属性将不再引用类的属性值.
2017-11-01
def count():
fs = []
for i in range(1, 4):
def f(j):
return j*j;
r = f(i);
fs.append(r)
return fs
f1, f2, f3 = count()
print f1,f2,f3
fs = []
for i in range(1, 4):
def f(j):
return j*j;
r = f(i);
fs.append(r)
return fs
f1, f2, f3 = count()
print f1,f2,f3
2017-11-01
def is_sqr(x):
return x**0.5 == int(x**0.5)
print filter(is_sqr, range(1, 101))
return x**0.5 == int(x**0.5)
print filter(is_sqr, range(1, 101))
2017-10-31
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count;
def __init__(self,name):
self.name = name
Person.__count +=1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
return cls.__count;
def __init__(self,name):
self.name = name
Person.__count +=1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2017-10-31
装饰器不难, 关键是要理解为什么要包裹这么多层. 本质是每一层函数的目的都是为了接收和处理参数.
所以装饰器使用起来也是需要多层传参, 而不是一次传入多个参数.
所以装饰器使用起来也是需要多层传参, 而不是一次传入多个参数.
2017-10-31
def is_sqr(x):
return math.sqrt(x) == round(math.sqrt(x))
return math.sqrt(x) == round(math.sqrt(x))
2017-10-27
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-10-27
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
2017-10-27
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 AttributeError:
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 AttributeError:
print 'attributeerror'
2017-10-27