类之_init_

作者: 心际花园 | 来源:发表于2017-06-02 08:53 被阅读2次

案例
正确写法

class CocaCola():
    formula = ['suagar','caffeine','water','soda']
    def __init__(self):
        self.local_logo='可口可乐'
coke =CocaCola()
print(coke.local_logo)

我的错误写法:

class CocaCola():
    formula = ['suagar','caffeine','water','soda']
    def __init__(self):
        local_logo='可口可乐'
coke =CocaCola()
print(coke.local_logo)

参考:侯爵的《编程小白的第一本Pyhton入门书》
学习的地方:
init的函数里local_logo前面没有加self

相关文章

  • 类之_init_

    案例正确写法 我的错误写法: 参考:侯爵的《编程小白的第一本Pyhton入门书》学习的地方:init的函数里loc...

  • 2020-01-15

    ''' # 在类中定义函数 class Student(object): # _init_第一参数是self,...

  • class用法学习

    class面向对象定义 错误写法: class people: #定义类为人 def _init_(sel...

  • 类8.类的魔术方法之_init_

    定义 init,全称为initialiaze,中文意思为初始化。init的说明(1)增加类的实例属性(2)增加类的...

  • 类的对象属性和init方法

    class 类名:属性方法实例一:init方法会自动被调用class Person: _ _ init_ _方法又...

  • python的魔法函数

    _init_() 所有类的超类object,有一个默认包含pass的init()实现,这个函数会在对象初始化的时候...

  • python 操作函数遇到的问题

    1. class 类下 _init_()方法 不能有返回值return,默认为None; 2. 如果调用class...

  • Python3 面向对象

    1. 类定义格式 2. 构造方法 def _init_(): 3. 私有属性 变量命名时在前面加入"__",如__...

  • Python3:类

    创建和使用类 _init_() 为构造函数(方法),每次创建实例时会自动调用。 self形参必不可少,且必须位于其...

  • python 魔法方法

    建立类的时候,需要使用_init_魔法方法,感到好奇,查阅资料后,总结一下。 _new_(cls[,...])当对...

网友评论

    本文标题:类之_init_

    本文链接:https://www.haomeiwen.com/subject/rbysfxtx.html