Python面向?qū)ο笾惡蛯ο髮傩缘脑鰟h改查操作示例
本文實(shí)例講述了Python面向?qū)ο笾惡蛯ο髮傩缘脑鰟h改查操作。分享給大家供大家參考,具體如下:
一、類屬性的操作
# -*- coding:utf-8 -*-
#! python2
class Chinese:
country = 'China'
def __init__(self,name):
self.name = name
def play_ball(self,ball):
print('%s play %s' %(self.name,ball))
#查看屬性
print(Chinese.country)
#修改屬性
Chinese.country = 'Japan'
print(Chinese.country)
p1 = Chinese('alex')
print(p1.__dict__)
print(p1.country)
#增加屬性
Chinese.dang = '腳本之家'
print(Chinese.dang)
print(p1.dang)
#刪除屬性
del Chinese.dang
del Chinese.country
print(Chinese.__dict__)
運(yùn)行結(jié)果:
China
Japan
{'name': 'alex'}
Japan
腳本之家
腳本之家
{'__module__': '__main__', 'play_ball': <function play_ball at 0x01AAB7B0>, '__doc__': None, '__init__': <function __init__ at 0x01AAB830>}
二、對象屬性的操作
# -*- coding:utf-8 -*-
#! python2
class Chinese:
country = 'China'
def __init__(self,name):
self.name = name
def play_ball(self,ball):
print('%s play %s' %(self.name,ball))
def test():
print("對象方法的屬性")
p1 = Chinese('alex')
print(p1.__dict__)
#查看屬性
print(p1.name)
print(p1.play_ball)
#增加屬性
p1.age = 18
print(p1.__dict__)
print(p1.age)
p1.test = test #將外界的方法作為函數(shù)屬性加入類中
print(p1.__dict__)
p1.test()
#修改屬性
p1.age = 19
print(p1.__dict__)
print(p1.age)
#刪除屬性
del p1.age
print(p1.__dict__)
運(yùn)行結(jié)果:
{'name': 'alex'}
alex
<bound method Chinese.play_ball of <__main__.Chinese instance at 0x01AE9DA0>>
{'age': 18, 'name': 'alex'}
18
{'test': <function test at 0x01AEB7F0>, 'age': 18, 'name': 'alex'}
對象方法的屬性
{'test': <function test at 0x01AEB7F0>, 'age': 19, 'name': 'alex'}
19
{'test': <function test at 0x01AEB7F0>, 'name': 'alex'}
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python數(shù)據(jù)類型轉(zhuǎn)換匯總
這篇文章主要給大家分享的是Python數(shù)據(jù)類型轉(zhuǎn)換匯總,int,float相互轉(zhuǎn)換、int,string相互轉(zhuǎn)換、float,string相互轉(zhuǎn)換、string,list相互轉(zhuǎn)換等常見內(nèi)容,需要的小伙伴可以參考一下2022-03-03
使用實(shí)現(xiàn)pandas讀取csv文件指定的前幾行
下面小編就為大家分享一篇使用實(shí)現(xiàn)pandas讀取csv文件指定的前幾行,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python人工智能之路 之PyAudio 實(shí)現(xiàn)錄音 自動化交互實(shí)現(xiàn)問答
關(guān)于音頻, PyAudio 這個庫, 可以實(shí)現(xiàn)開啟麥克風(fēng)錄音, 可以播放音頻文件等等。文章介紹了如何使用Python第三方庫PyAudio進(jìn)行麥克風(fēng)錄音然后自動播放已經(jīng)合成的語音實(shí)現(xiàn)語音交互回答,需要的朋友可以參考下2019-08-08
Pytorch 數(shù)據(jù)加載與數(shù)據(jù)預(yù)處理方式
今天小編就為大家分享一篇Pytorch 數(shù)據(jù)加載與數(shù)據(jù)預(yù)處理方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python常用標(biāo)準(zhǔn)庫詳解(pickle序列化和JSON序列化)
這篇文章主要介紹了Python常用標(biāo)準(zhǔn)庫,主要包括pickle序列化和JSON序列化模塊,通過使用場景分析給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Python實(shí)現(xiàn)簡單HTML表格解析的方法
這篇文章主要介紹了Python實(shí)現(xiàn)簡單HTML表格解析的方法,涉及Python基于libxml2dom模塊操作html頁面元素的技巧,需要的朋友可以參考下2015-06-06

