python之类与对象的属性

类相关的知识#

在python2中的区分:

经典类:

?
1
2
class School:
     pass

新式类:

?
1
2
class School( object ):
     pass

 在python3中以上两种均为新式类

属性:

  • 数据属性:就是变量
  • 函数属性:就是函数,在面向对象里通常称为方法

注:类和对象均用点来访问自己的属性

类的属性#

数据属性即变量,类的定义与函数又及其相似,其实可以用函数的作用域来理解类的属性调用

类的数据属性:

?
1
2
3
class School:
     Teacher = "老王"
print (School.Teacher)

类的函数属性(一般称为方法):

?
1
2
3
4
5
class School:
     Teacher = "老王"
     def Examination( self ):
         print ( "%s的班级正在考试" % self )
School.Examination( "alex" )

查看类属性:

查看类属性的两种方式:

  1. dir(类名) 查出的是一个名字列表
  2. 类名.__dict__ 查出的是一个字典,key是属性名,value是属性值

#注意:类名加点调用自己的属性实际上就是到属性字典里去找东西

复制代码
class School:
    Teacher = "老王"
    def Examination(self): print("%s的班级正在考试"%self) print(dir(School)) #查看类的属性 print(School.__dict__) #查看类的属性字典 print(School.Teacher)#同print(School.__dict__["Teacher"]) 调用数据属性 School.Examination("alex")#同School.__dict__["Examination"]("alex") 调用函数属性
复制代码

 特殊的类属性:

复制代码
class School:
    """
    文档
    """ Teacher = "老王" def Examination(self): print("%s的班级正在考试"%self) print(School.__name__) #查看类名 print(School.__doc__) #查看文档 print(School.__base__) #查看类的第一个父类 print(School.__bases__) #查看类所有父类构成的元组 print(School.__dict__) #查看类的属性 print(School.__module__) #查看类所在哪个模块 print(School.__class__) #实例School对应的类(仅新式类中)
复制代码

对象相关知识#

对象是由类实例化而来,实例化结果成为一个实例或者称作一个对象

实例化:

复制代码
class School:
    """
    文档
    """ Teacher = "老王" def Examination(self): print("%s的班级正在考试"%self) School() 

#类名加上括号就是实例化(可以理解函数运行的返回值就是一个实例)

复制代码

实例属性:

复制代码
class School:
    """
    这是一个学校类
    """ dang = "dang" #实例化,init函数不可以有返回值,返回值为空 def __init__(self,name,addr,type): self.mingzi = name #p1.mingzi = name self.dizhi = addr #p1.dizhi = addr self.leixing = type#p1.leixing = type #考试 def Examination(self): print("%s正在考试"%self.mingzi) #招生 def Recruit_students(self): print("%s正在招生"%self.mingzi) person = School("oldboy","沙河","私立") person.Examination() """ 1、实例化的过程本质上就是调用__init__的运行 2、self就是实例本身=person 3、class会自动帮__init__返回值 4、实例只有数据属性,print(person.__dict__) 5、实例调用函数属性,实际上调用的是类的函数属性,person.Examination() 6、print(person.dang)跟函数作用域一样,在自己的作用域找不到会去上一层找 7、类有数据属性和函数属性,实例只有数据属性 8、实例调用函数属性.class会帮你把实例本身传给函数 """
复制代码

作者: Lz12Code

出处:https://www.cnblogs.com/songzhixue/p/10058661.html

版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际」知识共享许可协议进行许可。

分类: python基础
<div id="blog_post_info">
            <div class="esa-sponsor">
                <a class="github" href="https://github.com/Lz12Code" target="_blank" title="Github"></a>
                <div class="text tr3">Sponsor</div>
                <ul class="box posa tr3">
                    <li class="paypal">PayPal</li>
                    <li class="alipay">AliPay</li>
                    <li class="wechat">WeChat</li>
                </ul>
                <div id="QRBox" class="posa left-100">
                    <div id="MainBox"></div>
                </div>
            </div>
0
0
<div class="clear"></div>
<div id="post_next_prev">

<a href="https://www.cnblogs.com/songzhixue/p/10041050.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/songzhixue/p/10041050.html" title="发布于 2018-11-29 21:39">python之面向对象编程</a>
<br>
<a href="https://www.cnblogs.com/songzhixue/p/10058678.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/songzhixue/p/10058678.html" title="发布于 2018-12-03 15:19">python之类与对象属性的增删改查</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值