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

import pyd 文件后无法找到其中定义的函数,求助

  •  
  •   oldbird · 2020-04-22 16:58:49 +08:00 · 1942 次点击
    这是一个创建于 1436 天前的主题,其中的信息可能已经有所发展或是发生改变。

    test.py 中定义了一个 hello 函数并编译成了 test.pyd ,

    将 test.pyd 拷到另一个脚本同目录下,import test 后,

    如果直接在脚本中 print hasattr(test,"hello")返回为 True,

    如果在脚本的某个类的方法中 print hasattr(test,"hello")返回为 False,

    不知道什么原因,请教。

    3 条回复    2020-04-23 07:34:45 +08:00
    chenstack
        1
    chenstack  
       2020-04-22 20:49:10 +08:00
    问的不清楚,在脚本的某个类的方法中 print hasattr(test,"hello"),这里的 test 是有定义了同名的变量覆盖了,直接 print(test)看下是啥
    EricCartmann
        2
    EricCartmann  
       2020-04-23 07:29:53 +08:00 via iPhone
    Python 文件、对象命名的时候千万不要用这类词语,非要用的话前面加个“my_”,你这八成都是名字出了问题,系统找到别的地方去了。
    EricCartmann
        3
    EricCartmann  
       2020-04-23 07:34:45 +08:00 via iPhone
    你在原始文件中可以用,是因为运行的时候导包从近到远,自身>同级目录>同工程>系统,移动到别处这个 test 不知道优先被当成谁了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1261 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 17:56 · PVG 01:56 · LAX 10:56 · JFK 13:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.