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-09-23
class Person(object):
def __init__(self,name,gender,birth,job):
self.name=name
self.gender=gender
self.birth=birth
self.job=job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
def __init__(self,name,gender,birth,job):
self.name=name
self.gender=gender
self.birth=birth
self.job=job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2017-09-23
class Person():
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming==xiaohong
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming==xiaohong
2017-09-23
from __future__ import unicode_literals ##future左边两根下划线,右边两根下划线。
s = 'am I an unicode?'
print isinstance(s, unicode)
s = 'am I an unicode?'
print isinstance(s, unicode)
2017-09-23
def count():
fs = []
for i in range(1, 4):
def f(i=i):
return i*i
fs.append(f)
#把参数i(一个局部变量)绑定到了当前外部的i变量的数值上,就可变了
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i=i):
return i*i
fs.append(f)
#把参数i(一个局部变量)绑定到了当前外部的i变量的数值上,就可变了
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-09-23
def cmp_ignore_case(s1, s2):
return cmp(s1.upper(),s2.upper())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return cmp(s1.upper(),s2.upper())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2017-09-22
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2017-09-22
# 这种叫什么来着...
class Fib(object):
def __init__(self,n):
self.n = n
def __call__(self):
a,b,L=0,1,[]
for i in range(self.n):
L.append(a)
a,b=b,a+b
return L
f = Fib(10)
print f()
class Fib(object):
def __init__(self,n):
self.n = n
def __call__(self):
a,b,L=0,1,[]
for i in range(self.n):
L.append(a)
a,b=b,a+b
return L
f = Fib(10)
print f()
2017-09-22
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-09-22
import os
print os.path.isdir(r'/data/webroot')
print os.listdir(r'/data/webroot/conf')
print os.path.isfile(r'/data/webroot/conf/app.conf')
print os.path.isdir(r'/data/webroot')
print os.listdir(r'/data/webroot/conf')
print os.path.isfile(r'/data/webroot/conf/app.conf')
2017-09-22
为什么不是在__init__里加判断呢?求解
if score < 0 or score > 100:
raise ValueError('Invalid score!')
if score < 0 or score > 100:
raise ValueError('Invalid score!')
2017-09-22