Python类/代码理解 [英] Python classes/ code understanding

查看:86
本文介绍了Python类/代码理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在pydev.org上发布了一个游戏.下面的代码是您为游戏运行所运行的代码.我的问题是

I recently downlaoded a game on pydev.org. The code below is the code that you run for the game to play. My question is does the likes of

os.path.dirname(__file__)

的赞是什么意思?我以前用python创建游戏,但是我总是将它们制作在一个类中.就像在游戏中一样,它在所有其他类所在的文件夹之外吗?

谁能帮我.我已经研究过了,但是没有运气!

mean? I have created games in python before but i always made them in the one class. Where as in this game this is outside of the folder where all the other classes are?

Can any1 help me. I have researched this but to no luck!

import sys
import os


try:
    __file__
except NameError:
    pass
else:
    libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), ''lib''))
    sys.path.insert(0, libdir)

sys.path.insert(1, "lib")
import main
main.main()

推荐答案

os.path.dirname(path) [< ^ ]是将返回文件的路径,其中给定的参数是完整路径,包括文件和可选的扩展名.
__file__(例如,请参见此处: http://docs.python.org/library/runpy.html [ ^ ])是一个特殊的全局变量,它将为包含瞬时执行的代码的文件的文件名.因此,如果您在代码中使用此变量,它将为您提供包含您编写的指令的文件名.

因此,有问题的构造将只为您提供包含瞬时执行指令的文件的路径.

问候,

曼弗雷德(Manfred)
os.path.dirname(path)[^] is a function that will return the path to a file where the parameter given is the complete path including the file and an optional extension.
__file__ (see here for instance: http://docs.python.org/library/runpy.html[^]) is a special global variable that will give the file name of the file that contains the momentarily executed code. So if you use this variable in your code it will give you the file name that contains the instructions you wrote.

So the construct in question will give you the path only of the file containing the momentarily executed instructions.

Regards,

Manfred


这篇关于Python类/代码理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆