推荐学习书目
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 · Apr 22, 2020 · 2604 views
    This topic created in 2239 days ago, the information mentioned may be changed or developed.

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

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

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

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

    不知道什么原因,请教。

    3 replies    2020-04-23 07:34:45 +08:00
    chenstack
        1
    chenstack  
       Apr 22, 2020
    问的不清楚,在脚本的某个类的方法中 print hasattr(test,"hello"),这里的 test 是有定义了同名的变量覆盖了,直接 print(test)看下是啥
    EricCartmann
        2
    EricCartmann  
       Apr 23, 2020 via iPhone
    Python 文件、对象命名的时候千万不要用这类词语,非要用的话前面加个“my_”,你这八成都是名字出了问题,系统找到别的地方去了。
    EricCartmann
        3
    EricCartmann  
       Apr 23, 2020 via iPhone
    你在原始文件中可以用,是因为运行的时候导包从近到远,自身>同级目录>同工程>系统,移动到别处这个 test 不知道优先被当成谁了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4835 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 04:01 · PVG 12:01 · LAX 21:01 · JFK 00:01
    ♥ Do have faith in what you're doing.