关于全局变量的初学者问题 [英] sort of a beginner question about globals

查看:69
本文介绍了关于全局变量的初学者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这本书并且也在搜索了这个小组

----------------------------- -----------------

我没有开始。

i希望从文件1中读取全局(OPTIONS)类方法
文件2中的
(func1)

i想要了解它是如何工作的。

------------ ----------------------------------

#file1.py

导入文件2

导入sys


OPTION = sys.argv [1:]

通过

a = global2.class1()

a.func1()

#file2.py

import __main__

通过

班级1:

.. def func1(个体经营):

..传递

-------------------------------------------

i have read the book and searched the group too
----------------------------------------------
im not gettin it.
i want to read a global (OPTIONS) from file1 from a class method
(func1) in file2
i want to understand how this works.
----------------------------------------------
#file1.py
import file2
import sys

OPTION = sys.argv[1:]
pass
a=global2.class1()
a.func1()
#file2.py
import __main__
pass
class class1:
.. def func1(self):
.. pass
-------------------------------------------

推荐答案

fred.dixon写道:
fred.dixon wrote:
我已经阅读了这本书并搜索了这个小组
----- -----------------------------------------
即时通讯没有得到它。
我想读全球(OPTI ONS)来自file1中的类方法
(func1)in file2
我想了解它是如何工作的。

------------- ---------------------------------
#file1.py
import file2
import sys

OPTION = sys.argv [1:]
传递
a = global2.class1()
a.func1()

#file2.py
导入__main__
传递
类class1:
。 def func1(个体经营):
。通过
-------------------------------------------
i have read the book and searched the group too
----------------------------------------------
im not gettin it.
i want to read a global (OPTIONS) from file1 from a class method
(func1) in file2
i want to understand how this works.

----------------------------------------------
#file1.py
import file2
import sys

OPTION = sys.argv[1:]
pass
a=global2.class1()
a.func1()
#file2.py
import __main__
pass
class class1:
. def func1(self):
. pass
-------------------------------------------




----- file1.py -----

导入文件2

a = file2.class1()

a.func1()

--------------------


----- file2.py -----

class class1(object):

def func1(self):< br $>
通过

--------------------


我不喜欢看不到你想要的OPTION,所以我已经把它删除了。如果你能够更好地解释你的意图,也许我可以帮助更多。


STeVe



----- file1.py -----
import file2

a = file2.class1()
a.func1()
--------------------

----- file2.py -----
class class1(object):
def func1(self):
pass
--------------------

I don''t see what you want OPTION for, so I''ve removed it. If you can
explain your intentions better, maybe I can help more.

STeVe


我想将OPTIONS用作全局变量。

在这种特殊情况下,我试图设置一个全局调试常量,所以我可以在我的程序中进行一些调试记录使用

-debug选项运行。

将在OPTIONS中最终实现的是OPTIONS.debug = True,因为我是使用optparse的
模块。

I want to use OPTIONS as a global var.
In this particular case I am trying to set a global debug constant so I
can have some debug logging happen when my program is run with a
-debug option.
what will actuall end up in OPTIONS is OPTIONS.debug = True as i am
using optparse module.


fred.dixon写道:
fred.dixon wrote:
我想将OPTIONS用作全局变量。
在此特殊情况我试图设置一个全局调试常量,所以当我的程序使用
-debug选项运行时,我可以进行一些调试记录。
将在OPTIONS中实现的最终结果是OPTIONS.debug = True我正在使用optparse模块。
I want to use OPTIONS as a global var.
In this particular case I am trying to set a global debug constant so I
can have some debug logging happen when my program is run with a
-debug option.
what will actuall end up in OPTIONS is OPTIONS.debug = True as i am
using optparse module.




为什么不把选项作为模块?


然后你可以做类似的事情:


----- options.py -----

debug = False

---------- ------------


----- file1.py -----

import file2

导入选项


options.debug = True

a = file2.class1()

a.func1( )

--------------------


----- file2.py - ----

导入选项

class class1(object):

def func1(个体经营):

如果options.debug:

打印''调试''

通过

-------------- ------


请注意,如果你想输出调试信息,你应该确实看一下日志模块。 b。 br />

STeVe



Why not just make options a module?

Then you can do something like:

----- options.py -----
debug = False
----------------------

----- file1.py -----
import file2
import options

options.debug = True
a = file2.class1()
a.func1()
--------------------

----- file2.py -----
import options
class class1(object):
def func1(self):
if options.debug:
print ''Debugging''
pass
--------------------

Note that if you''re trying to output debugging messges, you should
really look into the logging module instead.

STeVe


这篇关于关于全局变量的初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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