推荐学习书目
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
wellsc
V2EX  ›  Python

问个问题,面试官问你 “ Python 的对象方法是怎么存储的”,该怎么回答

  •  
  •   wellsc ·
    WyattJia · May 3, 2018 · 2147 views
    This topic created in 2962 days ago, the information mentioned may be changed or developed.
    不知道应该答 class method instance method 相关的知识,还是内存管理或 __slots__ 相关的知识...
    ilucio
        1
    ilucio  
       May 4, 2018
    感觉问的应该是类的知识。在类里定义一个方法,会在堆上开辟内存生成一个函数对象,并把这个对象名放在类的__dict__中。

    class apple(object):

    def price (self):
    print '2'

    def weight (self):
    print '400g'

    print apple.__dict__

    {'__module__': '__main__', 'weight': <function weight at 0x03868DB0>, 'price': <function price at 0x03868D70>, '__dict__': <attribute '__dict__' of 'apple' objects>, '__weakref__': <attribute '__weakref__' of 'apple' objects>, '__doc__': None}

    可见方法 price 和 weight 分别在堆上的不同位置。
    wellsc
        2
    wellsc  
    OP
       May 4, 2018
    @ilucio #1 我跟面试官确认过的,他说的对象是指类的实例,对象的方法是指 实例的 methods,刚开始我也答的是 __dict__ ,但是他说 dict 只是把对象属性返回出来了,而没有说清楚对象方法是怎么存储的。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1044 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:07 · PVG 07:07 · LAX 16:07 · JFK 19:07
    ♥ Do have faith in what you're doing.