python: __init__ function study notes


code example

color = 'red'
num = 23

#定义
class Color_Num:
    def __init__(self, color, num):
        self.color = color                 #颜色
        self.num = num                     #数字   
        
    def func_1(self):                      #函数1
        print(self.color)
        
    def func_2(self):                      #函数2
        self.num += 1    
        print(self.num)
    
#调用输出
p = Color_Num(color, num)
p.func_1                                  #不显示结果内容                        
p.func_1()                                #显示                           
p.func_2()

Note analysis

Insert image description here


Precautions

  • The __init__ function can only be called inside a class (class), and must be preceded by 'class class name:'
  • When calling output, you need to add parentheses after the function name, otherwise the output result will not be displayed.

Reference article

https://blog.csdn.net/weixin_48077303/article/details/106324830

Guess you like

Origin blog.csdn.net/qq_52671517/article/details/124628524