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
UN2758
V2EX  ›  Python

如何找到类和方法的实现?

  •  
  •   UN2758 · 2020-11-16 18:20:05 +08:00 · 1842 次点击
    这是一个创建于 1250 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1. socket 初始化之后 connect 方法和 getsockname 方法的实现位置怎么找啊?
    2. socket 类继承的是_socket.socket 父类,但是没看到父类的实现代码,这是猴子补丁吗,这样动不动就写个方法名,里面给个 pass 的类感觉很常见,但是找这种类的实现思路是啥呢?

    python 代码如下

    import socket
    
    
    def get_host_ip():
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(('8.8.8.8', 80))
            ip = s.getsockname()[0]
        finally:
            s.close()
    
        return ip
    
    13 条回复    2020-11-18 15:01:42 +08:00
    UN2758
        1
    UN2758  
    OP
       2020-11-16 18:20:47 +08:00
    wuwukai007
        2
    wuwukai007  
       2020-11-16 18:42:25 +08:00 via Android
    可能是 c 写的
    u823tg
        3
    u823tg  
       2020-11-16 19:30:27 +08:00
    这个不是可能是 c 写的。
    ho121
        4
    ho121  
       2020-11-16 19:37:44 +08:00 via Android
    类似 socket 这样的应该是调用的系统函数,具体实现实在系统中,当然没法直接看到源码
    DoctorCat
        6
    DoctorCat  
       2020-11-16 19:44:36 +08:00
    这种类是 stub,用来告诉你继承关系和方法属性的,真正的实现是在 C 写的扩展库_socket 中,如果不做个 stub,你就不知道这库都包含哪些对象和相关方法属性了。
    lithbitren
        7
    lithbitren  
       2020-11-17 04:10:55 +08:00
    一般是去官方文档对应模块页面,然后在页首找到源码,点进去以后就是 5 楼那堆仓库链接了,python 部分应该是可以直接跳转的,可以发先是_socket.socket 引进来的,所以直接在仓库里搜_socket 也是可以定位的。
    UN2758
        8
    UN2758  
    OP
       2020-11-17 09:32:08 +08:00
    @wuwukai007 #2
    @u823tg #3
    @ho121 #4
    @julyclyde #5
    @DoctorCat #6
    @lithbitren #7
    感谢各位大佬的帮助
    https://github.com/python/cpython/blob/master/Lib/socket.py#L51
    这个 socket.py 文件为啥不能从 IDE 里直接点击跳转找到呢?
    julyclyde
        9
    julyclyde  
       2020-11-17 10:20:29 +08:00
    @UN2758 IDE 是不是忽略标准库?
    rainfd
        10
    rainfd  
       2020-11-17 10:27:55 +08:00
    @UN2758 是你 ide 有问题吧,我用 vscode 能跳过去的
    no1xsyzy
        11
    no1xsyzy  
       2020-11-17 14:58:29 +08:00
    你是不是 import 路线上预编译的遮蔽了 .py
    UN2758
        12
    UN2758  
    OP
       2020-11-17 15:35:34 +08:00
    谢谢各位大佬,我看见了,但是真正的用 C 写的 SocketType 类是怎么加载到 python 里的呢,_socket.py 文件定义的是没实现方法的抽象类啊
    frostming
        13
    frostming  
       2020-11-18 15:01:42 +08:00
    @UN2758 楼上的回复你没好好看

    1. 实现是 C 语言,你跳不过去也看不了
    2. 你能跳过去只是因为 IDE(你用的大概是 Pycharm)放了一个 stub 文件,只是告诉你类里面有哪些成员,是个假的空壳
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2177 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 00:30 · PVG 08:30 · LAX 17:30 · JFK 20:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.