• __init__构造函数


    # -*- coding: utf-8 -*-
    class Document():
        def __init__(self, title, author, context):
         print('init function called')
         self.name='xxx'
    
    
    
    harry_potter_book = Document('Harry Potter', 'J. K. Rowling', '... Forever Do not believe any thing is capable of thinking independently ...')
    
    print(harry_potter_book.name)
    
    C:Python27python.exe "C:/Users/TLCB/PycharmProjects/untitled2/python study/t15.py"
    init function called
    xxx
    
    
    如果去掉__init__:
    
    # -*- coding: utf-8 -*-
    class Document():
        # def __init__(self, title, author, context):
         print('init function called')
         self.name='xxx'
    
    
    
    harry_potter_book = Document('Harry Potter', 'J. K. Rowling', '... Forever Do not believe any thing is capable of thinking independently ...')
    
    print(harry_potter_book.name)
    
    C:Python27python.exe "C:/Users/TLCB/PycharmProjects/untitled2/python study/t15.py"
    init function called
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled2/python study/t15.py", line 2, in <module>
        class Document():
      File "C:/Users/TLCB/PycharmProjects/untitled2/python study/t15.py", line 5, in Document
        self.name='xxx'
    NameError: name 'self' is not defined
  • 相关阅读:
    SQL获取分组第一条记录
    Highcharts中Legend动态显示点值
    Json序列化
    Xml 序列化
    Json 用法整理
    Oracle如何复制表的sql语句
    spring.net 如何让xml智能提示
    C# 属性和字段的区别
    EasyUI中Grid标题居中方法(jquery实现方法)
    Asp.net Web.Config
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348367.html
Copyright © 2020-2023  润新知