V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tanteng
V2EX  ›  Python

Python 的构造函数是__init__吧?

  •  
  •   tanteng ·
    tanteng · 2015-05-08 15:53:46 +08:00 · 3216 次点击
    这是一个创建于 3278 天前的主题,其中的信息可能已经有所发展或是发生改变。


    这个教程说init不是python构造函数,请各位专家大神说说。
    11 条回复    2015-05-21 13:20:30 +08:00
    thinker3
        1
    thinker3  
       2015-05-08 16:27:08 +08:00
    __new__, metaclass
    vinceguo
        2
    vinceguo  
       2015-05-08 17:07:59 +08:00
    python magic function, google
    staticor
        3
    staticor  
       2015-05-08 18:00:21 +08:00
    我个人理解是

    new: 你在内存的空间中找到一块空地, 先打个标签: 这地我占了.
    init : 你逛了一会儿 回到刚刚占的地, 开始盖了一房子, 用到的材料有 xxx xxx.

    所以若是说 **构造** , 应该侧重于前者的功能.
    rcmerci
        4
    rcmerci  
       2015-05-08 18:50:37 +08:00
    1. __new__
    2. 实例构造完成
    3. __init__ (初始化一些东西)
    lilydjwg
        5
    lilydjwg  
       2015-05-08 19:00:37 +08:00
    __new__ 才是构造方法,__init__ 是初始化方法。
    iEverX
        6
    iEverX  
       2015-05-08 19:12:30 +08:00
    感觉C#或者Java的构造函数,是 __new__ 和 __init__ 两个合起来
    monkeylyf
        7
    monkeylyf  
       2015-05-08 21:42:31 +08:00
    __init__是初始化 在执行这个的时候 实例已经被创建了 这和传统的constructor的概念是不一样的 (参考__new__)
    joshryo
        8
    joshryo  
       2015-05-09 00:13:23 +08:00
    python里面很多__xxx__方法都是很玄妙的东西,表面看着执行某函数/操作,其实后面都是执行的某个__xxx__方法
    slideclick
        9
    slideclick  
       2015-05-20 16:39:30 +08:00
    记住PPT内容,不要抠字眼。
    slideclick
        10
    slideclick  
       2015-05-21 12:03:19 +08:00
    你这ppt不错,哪里找的?
    tanteng
        11
    tanteng  
    OP
       2015-05-21 13:20:30 +08:00
    @slideclick 不是ppt,是我截图网页内容
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3238 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:39 · PVG 08:39 · LAX 17:39 · JFK 20:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.