import json
class Students(object):
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
class Students(object):
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
2017-08-20
print isinstance(t,Person),
print isinstance(t,Student),
print isinstance(t,Teacher),
print isinstance(t,object)
print isinstance(t,Student),
print isinstance(t,Teacher),
print isinstance(t,object)
2017-08-20
有Java、Android、PHP、Javascript......编程底子的人来学python都需要时间消化,因为不同语言语法、方法等都不同,但逻辑一样。不得不在这跟大家说掌握一门新的语言需要大量的练习、复习以及实战,掌握一门语言不是一刷过就行了。大家加油!——学而时习之,不亦说乎!
2017-08-20
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
@classmethod
def how_many(clx):
return clx.__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(clx):
return clx.__count
print Person.how_many(),
p1 = Person('Bob')
print Person.how_many()
2017-08-20
# -*- coding: utf-8 -*-
def get_grade(self):
if self.score >= 90:
return 'A-优秀'
elif self.score >= 60:
return 'B-合计'
else:
return 'C-不合计'
def get_grade(self):
if self.score >= 90:
return 'A-优秀'
elif self.score >= 60:
return 'B-合计'
else:
return 'C-不合计'
2017-08-20
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.__count,
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.__count,
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
2017-08-20
class Person(object):
count = 0
def __init__(self, name):
self.name = name
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 += 1
p1 = Person('Bob')
print Person.count,
p2 = Person('Alice')
print Person.count,
p3 = Person('Tim')
print Person.count
2017-08-20
按Java、PHP的来理解:
private __xxx
public xxx、__xxx__、_xxx
protected 还没学到,大家来补充
private __xxx
public xxx、__xxx__、_xxx
protected 还没学到,大家来补充
2017-08-20
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print p.__score
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print p.__score
2017-08-20
class Person(object):
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
for k, v in kw.iteritems():
setattr(self, k, v)
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
for k, v in kw.iteritems():
setattr(self, k, v)
2017-08-20
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, lambda p1, p2: cmp(p1.name.lower(), p2.name.lower()))
print L2[0].name,
print L2[1].name,
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, lambda p1, p2: cmp(p1.name.lower(), p2.name.lower()))
print L2[0].name,
print L2[1].name,
print L2[2].name
2017-08-20
from __future__ import unicode_literals
s = 'am I an unicode?'
print isinstance(s, unicode)
s = 'am I an unicode?'
print isinstance(s, unicode)
2017-08-20
try:
import json
except ImportError:
import simplejson
print json.dumps({'python':2.7})
import json
except ImportError:
import simplejson
print json.dumps({'python':2.7})
2017-08-20
def performance(unit):
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
return f(*args, **kw)
return wrapper
return decorator
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
return f(*args, **kw)
return wrapper
return decorator
2017-08-20