为了账号安全,请及时绑定邮箱和手机立即绑定

python进阶

廖雪峰 移动开发工程师
难度中级
时长 3小时33分
学习人数
综合评分9.20
575人评价 查看评价
9.6 内容实用
9.0 简洁易懂
9.0 逻辑清晰
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.iteritems():
setattr(Person,k,v)
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']
class BStudent(Student,BasketballMixin):
pass

class FTeacher(Teacher,FootballMixin):
pass
class BStudent(BasketballMixin):
pass

class FTeacher(FootballMixin):
pass
import json

class Students(object):
def read(self):
return r'["Tim", "Bob", "Alice"]'


s = Students()

print json.load(s)
class Teacher(Person):

def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course = course
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()
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,'-','不及格'
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'
class Person(object):
count = 0
def __init__(self,name):
self.name = name
Person.count += 1

最新回答 / 劳胜锋
因为不管哪个类最终都是继承object类,object类的初始化方法是__init__(self),所以我们只需要super(当前类,self).__init__()即可
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']

最新回答 / qazwsxedc_qaz
__repr__ 输出的内容给程序员看的   __str__ 给用户看的   当成java中的toString() 就好
类似java 的注解功能
#利用伪私有
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:')
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)
课程须知
本课程是Python入门的后续课程 1、掌握Python编程的基础知识 2、掌握Python函数的编写 3、对面向对象编程有所了解更佳
老师告诉你能学到什么?
1、什么是函数式编程 2、Python的函数式编程特点 3、Python的模块 4、Python面向对象编程 5、Python强大的定制类

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!

本次提问将花费2个积分

你的积分不足,无法发表

为什么扣积分?

本次提问将花费2个积分

继续发表请点击 "确定"

为什么扣积分?

举报

0/150
提交
取消