python中class(object)的含義是什么以及用法
python class(object)的含義
在python2中有區(qū)別,在Python3中已經(jīng)沒(méi)有區(qū)別:
object為默認(rèn)類,表示繼承關(guān)系
class Person: ? ? name = "zhengtong" class Animal(object): ? ? name = "chonghong" ? if __name__ == "__main__": ? ? x = Person() ? ? print( "Person", dir(x)) ? ? ? y = Animal() ? ? print ("Animal", dir(y))
Python3中運(yùn)行結(jié)果:
person [‘class’, ‘delattr’, ‘dict’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘lt’, ‘module’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘weakref’, ‘name’]
animal [‘class’, ‘delattr’, ‘dict’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘lt’, ‘module’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘weakref’, ‘name’]
class, class()和class(object)的區(qū)別
為什么要繼承object類
Python2中,遇到 class A 和 class A(object) 是有概念上和功能上的區(qū)別的,分別稱為經(jīng)典類(舊式類,old-style)與新式類(new-style)的區(qū)別。python2中為什么在進(jìn)行類定義時(shí)最好要加object,加 & 不加如下實(shí)例。
歷史進(jìn)程:2.2以前的時(shí)候type和object還不統(tǒng)一. 在2.2統(tǒng)一到3之間, 要用class
- Foo(object)來(lái)申明新式類, 因?yàn)樗膖ype是 < type ‘type’ > 。
- 不然的話, 生成的類的type就是 <type ‘classobj’ >。
繼承object類的原因:主要目的是便于統(tǒng)一操作。
- 在python 3.X中已經(jīng)默認(rèn)繼承object類。
# -.- coding:utf-8 -.-
# __author__ = 'zhengtong'
class Person:
"""
不帶object
"""
name = "zhengtong"
class Animal(object):
"""
帶有object
"""
name = "chonghong"
if __name__ == "__main__":
x = Person()
print "Person", dir(x)
y = Animal()
print "Animal", dir(y)
Person ['__doc__', '__module__', 'name']
Animal ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
Person類很明顯能夠看出區(qū)別,不繼承object對(duì)象,只擁有了doc , module 和 自己定義的name變量, 也就是說(shuō)這個(gè)類的命名空間只有三個(gè)對(duì)象可以操作。
Animal類繼承了object對(duì)象,擁有了好多可操作對(duì)象,這些都是類中的高級(jí)特性。
class, class()和class(object)的區(qū)別
python2中寫為如下兩種形式都是不能繼承object類的,也就是說(shuō)是等價(jià)的。
def class: def class():
繼承object類是為了讓自己定義的類擁有更多的屬性,以便使用。當(dāng)然如果用不到,不繼承object類也可以。
python2中繼承object類是為了和python3保持一致,python3中自動(dòng)繼承了object類。
python2中需要寫為如下形式才可以繼承object類。
def class(object):
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python?TypeError:?‘float‘?object?is?not?subscriptable錯(cuò)誤解決
- 最新整理Python中的type和object的示例詳解
- Python開(kāi)發(fā)時(shí)報(bào)TypeError:?‘int‘?object?is?not?iterable錯(cuò)誤的解決方式
- Python源碼學(xué)習(xí)之PyType_Type和PyBaseObject_Type詳解
- Python源碼學(xué)習(xí)之PyObject和PyTypeObject
- python源碼剖析之PyObject詳解
- 關(guān)于Python中object類特殊方法的解釋
相關(guān)文章
Python計(jì)算點(diǎn)到直線距離、直線間交點(diǎn)夾角
這篇文章主要介紹了Python計(jì)算點(diǎn)到直線距離、直線間交點(diǎn)夾角,需要的朋友可以參考下2021-12-12
python虛擬環(huán)境多種創(chuàng)建方式圖文詳解
創(chuàng)建虛擬環(huán)境是為了讓項(xiàng)目運(yùn)行在一個(gè)獨(dú)立的局部的Python環(huán)境中,使得不同環(huán)境的項(xiàng)目互不干擾,這篇文章主要給大家介紹了關(guān)于python虛擬環(huán)境多種創(chuàng)建方式的相關(guān)資料,需要的朋友可以參考下2024-08-08
Python+Selenium自動(dòng)化實(shí)現(xiàn)分頁(yè)(pagination)處理
這篇文章主要為大家詳細(xì)介紹了Python+Selenium自動(dòng)化實(shí)現(xiàn)分頁(yè)pagination處理的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
python爬蟲(chóng) 線程池創(chuàng)建并獲取文件代碼實(shí)例
這篇文章主要介紹了python爬蟲(chóng) 線程池創(chuàng)建并獲取文件代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python運(yùn)用于數(shù)據(jù)分析的簡(jiǎn)單教程
這篇文章主要介紹了Python運(yùn)用于數(shù)據(jù)分析的簡(jiǎn)單教程,主要介紹了如何運(yùn)用Python來(lái)進(jìn)行數(shù)據(jù)導(dǎo)入、變化、統(tǒng)計(jì)和假設(shè)檢驗(yàn)等基本的數(shù)據(jù)分析,需要的朋友可以參考下2015-03-03
jupyter 使用Pillow包顯示圖像時(shí)inline顯示方式
這篇文章主要介紹了jupyter 使用Pillow包顯示圖像時(shí)inline顯示方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

