免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2552 | 回复: 2
打印 上一主题 下一主题

python类的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-09-10 10:10 |只看该作者 |倒序浏览
刚学python,问个简单问题
我定义了一个类,那么我怎么引用呢?
  1. [root@python testclass]# ls -l
  2. total 8
  3. -rw-r--r-- 1 root root  38 Sep 10 02:08 test.py
  4. -rw-r--r-- 1 root root 270 Sep 10 02:09 test.pyc
  5. [root@python testclass]# cat test.py
  6. #!/usr/bin/python
  7. class person:
  8.         pass
  9. [root@python testclass]# python
  10. Python 2.6.6 (r266:84292, Sep 12 2011, 14:03:14)
  11. [GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
  12. Type "help", "copyright", "credits" or "license" for more information.
  13. >>> p=person()
  14. Traceback (most recent call last):
  15.   File "<stdin>", line 1, in <module>
  16. NameError: name 'person' is not defined
  17. >>> import test
  18. >>> p=person()
  19. Traceback (most recent call last):
  20.   File "<stdin>", line 1, in <module>
  21. NameError: name 'person' is not defined
  22. >>> import test.py
  23. Traceback (most recent call last):
  24.   File "<stdin>", line 1, in <module>
  25. ImportError: No module named py
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-09-10 10:16 |只看该作者
已经解决。。p = test.person()
即可。。。。。

论坛徽章:
8
技术图书徽章
日期:2013-08-22 11:21:28未羊
日期:2015-01-19 22:22:25巳蛇
日期:2014-08-11 16:53:08子鼠
日期:2014-05-29 09:04:44摩羯座
日期:2014-04-11 14:15:07丑牛
日期:2014-01-24 12:41:28金牛座
日期:2013-11-21 17:38:28射手座
日期:2015-01-21 08:50:32
3 [报告]
发表于 2015-09-10 15:35 |只看该作者
import test 而不是import test.py
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP