def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.iteritems():
setattr(Person,k,v)
self.name = name
self.gender = gender
for k,v in kw.iteritems():
setattr(Person,k,v)
2018-02-27
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
if 'age' in kw:
self.age = kw['age']
if 'course' in kw:
self.course = kw['course']
self.name = name
self.gender = gender
if 'age' in kw:
self.age = kw['age']
if 'course' in kw:
self.course = kw['course']
2018-02-27
class BStudent(Student,BasketballMixin):
pass
class FTeacher(Teacher,FootballMixin):
pass
pass
class FTeacher(Teacher,FootballMixin):
pass
2018-02-27
class BStudent(BasketballMixin):
pass
class FTeacher(FootballMixin):
pass
pass
class FTeacher(FootballMixin):
pass
2018-02-27
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)
2018-02-27
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course = course
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course = course
2018-02-27
class Person(object):
__count = 0
@classmethod
def how_many(s):
return s.__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(s):
return s.__count
def __init__(self,name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-02-27
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score > 80:
print self.name,'-','优秀'
elif self.__score > 60:
print self.name,'-','及格'
else:
print self.name,'-','不及格'
self.name = name
self.__score = score
def get_grade(self):
if self.__score > 80:
print self.name,'-','优秀'
elif self.__score > 60:
print self.name,'-','及格'
else:
print self.name,'-','不及格'
2018-02-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:
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
最新回答 / 劳胜锋
因为不管哪个类最终都是继承object类,object类的初始化方法是__init__(self),所以我们只需要super(当前类,self).__init__()即可
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