GOT是更好的方式! [英] There's GOT to be a better way!

查看:72
本文介绍了GOT是更好的方式!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个程序,我称之为自定义模块。 ''global''

命令似乎不适用,所以如何在模块中内部更改一个变量

而不将其传递给n层,并且再回来了?


厄尔

解决方案

Earl Eiland写道:

我正在编写我的第一个程序,我称之为自定义模块。 ''global''
命令似乎不适用,所以如何在模块中内部更改变量而不将其传递给n层,然后再返回?


你认为全局不是你想要的是正确的 - 它真的是

意味着全局到它出现的模块命名空间。 />

但是,如果两个单独的代码段都可以引用相同的

模块,则可以在模块中设置属性,另一个可以

引用它。不要忘记,当您导入模块时,其名称

在导入模块中变为全局。由于模块只是一个美元名称空间,所以任何可以引用该模块的东西都可以读取
和/或设置该模块的属性。


a.py:


导入一些东西

something.x ="一个值


b.py:


进口东西

打印东西。


将打印A值"只要a是在b之前导入的。


问候

Steve

-

见面Python开发人员和你的clpy收藏夹March 23-25

来到PyCon DC 2005 http://www.pycon.org/

Steve Holden http://www.holdenweb.com/


在星期四,2005-03-03 15:11,史蒂夫霍尔登写道:< blockquote class =post_quotes> Earl Eiland写道:

我正在编写我的第一个程序,我称之为自定义模块。 ''global''
命令似乎不适用,所以如何在模块中内部更改变量而不将其传递给n层,然后再返回?

你认为全局不是你想要的是正确的 - 它确实意味着它出现在模块名称空间的全局。

但是,如果两个独立的代码段都可以引用相同的模块,则可以在模块中设置属性,另一个可以引用它。不要忘记,当您导入模块时,其名称
在导入模块中变为全局。由于模块只是一个美化名称空间,任何可以引用该模块的东西都可以读取和/或设置该模块的属性。

a.py:

导入一些东西
something.x ="一个值

b.py:

导入一些东西
打印东西。 x

将打印A值只要a在b之前导入。



对。那部分我想通了。如何在导入的

模块中的一个函数访问同一模块中的变量?


module.py

def A( ):

test = 1

for x in range(10):B()


def B():

test = test + 1

main.py

导入模块

module.A()

这将失败,除非测试通过并返回。


Earl Eiland写道:

2005年4月2日-03-03在15:11,Steve Holden写道:

Earl Eiland写道:

我正在编写我的第一个程序我称之为自定义模块。 ''global''
命令似乎不适用,那么如何在模块中内部更改变量而不将其传递给n层,然后再次退出?

你认为全局不是你想要的是正确的 - 它确实意味着它出现在模块名称空间的全局。

但是,如果两个独立的代码段都可以引用相同的模块,则可以在模块中设置属性,另一个可以引用它。不要忘记,当您导入模块时,其名称
在导入模块中变为全局。由于模块只是一个美化名称空间,任何可以引用该模块的东西都可以读取和/或设置该模块的属性。

a.py:

导入一些东西
something.x ="一个值

b.py:

导入一些东西
打印东西。 x

将打印A值只要a在b之前导入。



对。那部分我想通了。如何在导入的
模块中访问同一模块中的变量?

module.py
def A():



全局测试测试= 1
x范围内(10):B()

def B():
全局测试测试=测试+ 1

main.py
导入模块
module.A()
print module.test

这将失败,除非传递并返回测试。




问候

Steve

-

认识Python开发人员和你的cl py收藏3月23 - 25日

来到PyCon DC 2005 http:// www.pycon.org/

Steve Holden http:/ /www.holdenweb.com/


I''m writing my first program where I call custom modules. The ''global''
command doesn''t seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?

Earl

解决方案

Earl Eiland wrote:

I''m writing my first program where I call custom modules. The ''global''
command doesn''t seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?


You are correct in assuming that global isn''t what you want - it really
means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don''t forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module''s attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/


On Thu, 2005-03-03 at 15:11, Steve Holden wrote:

Earl Eiland wrote:

I''m writing my first program where I call custom modules. The ''global''
command doesn''t seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?


You are correct in assuming that global isn''t what you want - it really
means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don''t forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module''s attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.


Right. That part I figured out. How does one function in an imported
module access a variable in the same module?

module.py
def A():
test = 1
for x in range(10): B()

def B():
test = test + 1
main.py
import module
module.A()
This will fail, unless test is passed and returned.


Earl Eiland wrote:

On Thu, 2005-03-03 at 15:11, Steve Holden wrote:

Earl Eiland wrote:

I''m writing my first program where I call custom modules. The ''global''
command doesn''t seem to apply, so how do I change a variable internally
in a module without passing it down n layers, and then back out again?

You are correct in assuming that global isn''t what you want - it really
means "global to the module namespace in which it appears".

However, if two separate pieces of code can both reference the same
module then one can set an attribute in the module and the other can
reference it. Don''t forget that when you import a module its name
becomes global within the importing module. Since a module is just a
glorified namespace, anything that can reference the module can read
and/or set that module''s attributes.

a.py:

import something
something.x = "A value"

b.py:

import something
print something.x

will print "A value" as long as a is imported before b.



Right. That part I figured out. How does one function in an imported
module access a variable in the same module?

module.py
def A():


global test test = 1
for x in range(10): B()

def B(): global test test = test + 1
main.py
import module
module.A() print module.test

This will fail, unless test is passed and returned.



regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/


这篇关于GOT是更好的方式!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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