不歸路系列:Python入門之旅-一定要注意縮進(jìn)?。。。ㄍ扑])
因?yàn)楣ぷ鳎☉卸瑁?,幾年了,斷斷續(xù)續(xù)學(xué)習(xí)又半途而廢了一個(gè)又一個(gè)技能。試著開始用博客記錄學(xué)習(xí)過程中的問題和解決方式,以便激勵(lì)自己和順便萬一幫助了別人呢。
最近面向?qū)ο髮懥藗€(gè)Python類,到訪問限制(私有屬性)時(shí)竟然報(bào)錯(cuò),好多天百思不得其姐,沒啥破綻?。〈a如下,可就是報(bào)錯(cuò)!(后面有報(bào)錯(cuò)截圖)
class Person(object):
def run(self):
print("run")
def eat(self,food):
print("eat " + food)
def say(self):
print("My name is %s,I am %d years old" % (self.name,self.age))
# 構(gòu)造函數(shù),創(chuàng)建對(duì)象時(shí)默認(rèn)的初始化
def __init__(self,name,age,height,weight,money):
self.name = name
self.age = age
self.height = height
self.weight = weight
self.__money = money #實(shí)際上是_Person__money
print("哈嘍!我是%s,我今年%d歲了。目前存款%f" %(self.name,self.age,self.__money))
# 想要內(nèi)部屬性不被直接外部訪問,屬性前加__,就變成了私有屬性private
self.__money = 100
# 私有屬性需要定義get、set方法來訪問和賦值
def setMoney(self,money):
if(money < 0):
self.__money = 0
else:
self.__money = money
def getMoney(self):
return self.__money
person = Person("小明", 5, 120, 28,93.1)
# 屬性可直接被訪問
person.age = 10
print(person.age)
# 私有屬性不可直接被訪問或賦值,因?yàn)榻忉屍靼裚_money變成了_Person__money(可以用這個(gè)訪問到私有屬性的money,但是強(qiáng)烈建議不要),以下2行會(huì)報(bào)錯(cuò)
# person.money = 10
# print(person.__money)
# 可以調(diào)用內(nèi)部方法訪問和賦值
print(person.getMoney())
person.setMoney(-10)
print(person.getMoney())
Excuse me?!咋個(gè)就沒有,那不上面大大擺著倆內(nèi)部方法嘛!

昨天看著看著突然迸發(fā)了個(gè)小火星子,想起來縮進(jìn)不對(duì)了,如圖:

把兩個(gè)方法減一個(gè)縮進(jìn),就算是出來了,是類的方法,和__init__并列了,自然就正確了。
class Person(object):
def run(self):
print("run")
def eat(self,food):
print("eat " + food)
def say(self):
print("My name is %s,I am %d years old" % (self.name,self.age))
# 構(gòu)造函數(shù),創(chuàng)建對(duì)象時(shí)默認(rèn)的初始化
def __init__(self,name,age,height,weight,money):
self.name = name
self.age = age
self.height = height
self.weight = weight
self.__money = money #實(shí)際上是_Person__money
print("哈嘍!我是%s,我今年%d歲了。目前存款%f" %(self.name,self.age,self.__money))
# 想要內(nèi)部屬性不被直接外部訪問,屬性前加__,就變成了私有屬性private
self.__money = 100
# 私有屬性需要定義get、set方法來訪問和賦值
def setMoney(self, money):
if (money < 0):
self.__money = 0
else:
self.__money = money
def getMoney(self):
return self.__money
person = Person("小明", 5, 120, 28,93.1)
# 屬性可直接被訪問
person.age = 10
print(person.age)
# 私有屬性不可直接被訪問或賦值,因?yàn)榻忉屍靼裚_money變成了_Person__money(可以用這個(gè)訪問到私有屬性的money,但是強(qiáng)烈建議不要),以下2行會(huì)報(bào)錯(cuò)
# person.money = 10
# print(person.__money)
# 可以調(diào)用內(nèi)部方法訪問和賦值
print(person.getMoney())
person.setMoney(-10)
print(person.getMoney())

總結(jié)下:一定要細(xì)心!細(xì)心?。≡偌?xì)心?。?!
注意縮進(jìn)
注意縮進(jìn)
注意縮進(jìn)
以上所述是小編給大家介紹的Python入門一定要注意縮進(jìn)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Python中使用aiohttp模擬服務(wù)器出現(xiàn)錯(cuò)誤問題及解決方法
這篇文章主要介紹了Python中使用aiohttp模擬服務(wù)器出現(xiàn)錯(cuò)誤,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Python實(shí)現(xiàn)透明數(shù)字時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了一個(gè)使用 Python 和 Tkinter 庫實(shí)現(xiàn)的透明數(shù)字時(shí)鐘應(yīng)用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2025-02-02
python?time模塊計(jì)算時(shí)間之間的差距(練習(xí)題)
這篇文章主要介紹了python?time模塊計(jì)算時(shí)間之間的差距,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

