将[class-scope]变量传递给方法之间? [英] passing [class-scope] variables to between methods?

查看:55
本文介绍了将[class-scope]变量传递给方法之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计


我知道我遗漏了一些关于如何使用python语言的要点。是否有可能在方法之间传递变量(这就是它们被称为什么?)


在我的尝试下面我有一个初始定义,用户在2个文本字段中输入并在以后使用一旦按钮GO !!!按钮创建一堆目录的脚本。单击。


我收到此错误:
jobfolder = a

NameError:全局名称''a''未定义



但我在剧本开头定义了它。


有什么想法吗?


对所有问题论坛帖子表示歉意 - 我很快就会停止!


AT


Hey Guys

I know I am missing some important points on how to use the python language. Is it possible to pass variables between methods (is that what theyare called?)

in my attempt below I have an initial definition that gets users input in 2 text fields and uses that later down the script to create a bunch of directories once the button "GO!!!" is clicked.

I get this error:
jobfolder = a
NameError: global name ''a'' is not defined


but i defined it up in the beginning of the script.

Any ideas?

Apologies for all the questions forum posts - I''ll stop soon!

AT


展开 | 选择 | Wrap | 行号

推荐答案

这是关于范围的全部内容。变量。

在__init __()函数中,变量a只有该范围内的生命(方法 - 或函数,如果您愿意 - 范围)。指向节目的对象是Dialog。 self(this Dialog)存在于每个方法定义中的原因是,给定方法可以访问对象范围(self.whatever)。因此,如果您希望变量存在于函数外部,请将其作为对象的属性:
It''s all about the "scope" of a variable.
Inside your __init__() function, the variable a only has life in that scope (the method - or function, if you prefer- scope). The object that is directing the show is the Dialog. The reason that self (this Dialog) is present in every method definition is so that the given method has access to the object scope (self.whatever). So, if you want a variable to live outside a function, make it an attribute of the object:
展开 | 选择 | Wrap | 行号





将变量附加到你的班级,


第15行:self.myVariable


然后在方法


def OnCreateJob():

self.myVariable ...


br,

Bj?rn
Hi,

attach the variable to your class,

line 15: self.myVariable

then in the method

def OnCreateJob():
self.myVariable...

br,
Bj?rn


我注意到你正在以一种有争议的方式做几件事。

1)将ID作为绝对值分配是一个明确的禁忌。它必将引发问题:
I notice that you are doing several things in a debatable manner.
1) Assigning IDs as absolutes is a definite no-no. It''s bound to cause problems down the road:
展开 | 选择 | Wrap | 行号


这篇关于将[class-scope]变量传递给方法之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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