新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python類屬性設(shè)置默認(rèn)值
類屬性說明

1、類中的所有屬性都必須有初始值,即使這個值是0或空字符串。
2、在某些情況下,設(shè)定默認(rèn)值時,在方法__init_()內(nèi)指定該初始值是可行的,如果對某個屬性進(jìn)行這樣的話,就不需要提供初始值的形參。
實例
class Cat():
def __init__(self, name, color):
self.name = name
self.color = color
self.age = 3
def eat(self):
print('cat ' + self.name + ' color ' + self.color + ', now eat')
def run(self):
print('cat ' + self.name + ' color ' + self.color + ', now run')
def print_age(self):
print('cat`s age is ' + str(self.age))以上就是python類屬性設(shè)置默認(rèn)值的方法,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:python類屬性設(shè)置默認(rèn)值
網(wǎng)站地址:http://fisionsoft.com.cn/article/dhcopjd.html


咨詢
建站咨詢
