无法从函数传递变量 [英] cannot pass a variable from a function

查看:221
本文介绍了无法从函数传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手。这应该是一个简单的答案,但我不能得到

这个工作。代码如下所示。我知道怎么用C,

Fortran和VB做这个,但它似乎在这里的工作方式不一样。

我将不胜感激任何帮助。


#try这个将一个列表传递给一个函数并让函数返回

#a variable

#this works

list = [1,4,6,9]

def fctn(c):

for c in c:

q = h * 80

打印q

#function假设返回变量

def fctn2(c):
$ b c中h为$ b:

q = h * 80

返回q

def prntfctn(y):

for j in y:

print j

fctn(list)

fctn2(list)

prntfctn( q)


我需要能够从函数中返回变量,以便在我正在编写的程序的其余部分中全局使用它们./ br
。 />
谢谢


Doug

I am fairly new to Python. This should be an easy answer but I cannot get
this to work. The code is listed below. I know how to do this in C,
Fortran, and VB but it doesn''t seem to work the same way here.
I would appreciate any help.

#try this to pass a list to a function and have the function return
#a variable
#this works
list=[1,4,6,9]
def fctn(c):
for h in c:
q=h*80
print q
#function suppose to return variable
def fctn2(c):
for h in c:
q=h*80
return q
def prntfctn(y):
for j in y:
print j
fctn(list)
fctn2(list)
prntfctn(q)

I need to be able to return variables from functions so they can be used
globally in the rest of the program I am writing.
Thanks

Doug

推荐答案

-----开始PGP签名留言-----

哈希:SHA1


2004-06-16T 22:46:42Z,Doug Jordan < DJ ****** @ houston.rr.com>写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2004-06-16T22:46:42Z, "Doug Jordan" <dj******@houston.rr.com> writes:
#function假设返回变量
def fctn2(c):
for c in c:
q = h * 80
def prntfctn(y):
对于j的y:
打印j

fctn2(列表)
prntfctn (q)
#function suppose to return variable
def fctn2(c):
for h in c:
q=h*80
return q

def prntfctn(y):
for j in y:
print j

fctn2(list)
prntfctn(q)




名称q仅存在于fctn2变量的范围内。如果你想要它存在于全局范围内,请将其分配到那里:


q = fctn2(list)

prtnfctn(q )


这应该做你想要的。请注意,我不知道任何现代的

编程语言,它允许函数在没有明确请求的情况下为

全局变量赋值。如果存在这样的事情,那么我强烈建议你不惜一切代价避免它。

- -

Kirk Strauser

Strauser Group

开放。解决方案。简单。
http://www.strausergroup.com/

----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.2.4(GNU / Linux)


iD8DBQFA0NS95sRg + Y0CpvERAlGRAKCkUJTaBJIckaWCvM2qkE mA8BDSEgCaAgcp

u44PX2uPlSMGYAV4VG5jaC8 =

= G3qn

----- END PGP SIGNATURE -----



The name "q" only exists inside the scope of the fctn2 variable. If you
want it present inside the global scope, assign it there:

q = fctn2(list)
prtnfctn(q)

That should do what you want. Note that I''m unaware of any modern
programming language that would allow a function to assign a value to a
global variable without explicitly requesting it. If such a thing exists,
then I highly recommend you avoid it at all costs.
- --
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA0NS95sRg+Y0CpvERAlGRAKCkUJTaBJIckaWCvM2qkE mA8BDSEgCaAgcp
u44PX2uPlSMGYAV4VG5jaC8=
=G3qn
-----END PGP SIGNATURE-----


Kirk,

感谢您的投入,这不是我想要做的事情。

我明白q是本地的范围。我试图返回q并使用另一个具有全局范围的变量对该函数进行

调用。


其他语言

子程序foo(b,c)

c = b * 1000

返回

来电foo(q,r)

其中q和r是定义和类型相同的b,c作为函数

如何在python中执行此操作。我需要对一个变量执行操作,并且

将新变量传递给程序。

希望这可能会清除它。


Doug

" Kirk Strauser" <き** @ strauser.com>在留言中写道

news:87 ************ @ strauser.com ...

-----开始PGP签名留言-----

哈希:SHA1


2004-06-16T22:46:42Z,Doug Jordan < DJ ****** @ houston.rr.com>写道:
Kirk,
Thanks for your input, hoever that is not exactly what I am trying to do.
I understand that q is local scope. I was trying to return q and make a
call to the function using another variable with global scope.

In other language
subroutine foo(b,c)
c=b*1000
return
call foo(q,r)
where q and r are defines and same type as b,c as function
How do I do this in python. I need to perform operations on a variable and
pass the new variable to the program.
Hope this might clear it up.

Doug
"Kirk Strauser" <ki**@strauser.com> wrote in message
news:87************@strauser.com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2004-06-16T22:46:42Z, "Doug Jordan" <dj******@houston.rr.com> writes:
#function假设返回变量
def fctn2(c):
for c in c:
q = h * 80
def prntfctn(y):
对于j的y:
打印j

fctn2(列表)
prntfctn (q)
#function suppose to return variable
def fctn2(c):
for h in c:
q=h*80
return q

def prntfctn(y):
for j in y:
print j

fctn2(list)
prntfctn(q)




名称q仅存在于fctn2变量的范围内。如果你想要它存在于全局范围内,请将其分配到那里:


q = fctn2(list)

prtnfctn(q )


这应该做你想要的。请注意,我不知道任何现代的

编程语言,它允许函数在没有明确请求的情况下为

全局变量赋值。如果存在这样的事情,那么我强烈建议你不惜一切代价避免它。

- -

Kirk Strauser

Strauser Group

开放。解决方案。简单。
http://www.strausergroup.com/

----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.2.4(GNU / Linux)


iD8DBQFA0NS95sRg + Y0CpvERAlGRAKCkUJTaBJIckaWCvM2qkE mA8BDSEgCaAgcp

u44PX2uPlSMGYAV4VG5jaC8 =

= G3qn

----- END PGP SIGNATURE -----



The name "q" only exists inside the scope of the fctn2 variable. If you
want it present inside the global scope, assign it there:

q = fctn2(list)
prtnfctn(q)

That should do what you want. Note that I''m unaware of any modern
programming language that would allow a function to assign a value to a
global variable without explicitly requesting it. If such a thing exists,
then I highly recommend you avoid it at all costs.
- --
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA0NS95sRg+Y0CpvERAlGRAKCkUJTaBJIckaWCvM2qkE mA8BDSEgCaAgcp
u44PX2uPlSMGYAV4VG5jaC8=
=G3qn
-----END PGP SIGNATURE-----


Doug,


您正在谈论通过引用传递。 Python

不会这样做。它只传递值,除非你传递一个对象(例如列表,字典,类等)。

在这些情况下你可以修改函数中的对象。


对于简单的操作,只需返回值使用

稍后(如Fortran函数)。


def foo(b)

返回b * 1000

c = foo(b)


对象可以通过并且修改了


def foo(b,l)

l.append(b)

返回


l = []

foo(1)

l-> [1]

foo(2)

l-> [1,2]

foo(''test'')

l-> [1,2,''测试'']


HTH,

拉里贝茨

Syscon,Inc。


Doug Jordan < DJ ****** @ houston.rr.com>在消息中写道

新闻:51 *************** @ fe2.texas.rr.com ...
Doug,

You are talking about passing by reference. Python
doesn''t do that. It only passes by value, unless you
pass an object (e.g. list, dictionary, class, etc.).
In those cases you CAN modify object in the function.

For simple operations, just return the value an use
it later (like Fortran functions).

def foo(b)
return b*1000

c=foo(b)

objects can be passed and modified

def foo(b, l)
l.append(b)
return

l=[]
foo(1)
l->[1]
foo(2)
l->[1,2]
foo(''test'')
l->[1,2,''test'']

HTH,
Larry Bates
Syscon, Inc.

"Doug Jordan" <dj******@houston.rr.com> wrote in message
news:51***************@fe2.texas.rr.com...
Kirk,
感谢您的意见,这并不是我想要做的事情。
我知道q是本地范围。我试图返回q并使用另一个具有全局范围的变量来调用该函数。

用其他语言
子程序foo(b,c)
c = b * 1000
返回
调用foo(q,r)
其中q和r的定义和类型相同,b,c作为函数
我如何在蟒蛇。我需要对变量
执行操作并将新变量传递给程序。
希望这可以清除它。

Doug
Kirk Strauser <き** @ strauser.com>在消息中写道
新闻:87 ************ @ strauser.com ......
-----开始PGP签名消息-----
Hash:SHA1

2004-06-16T22:46:42Z,Doug Jordan < DJ ****** @ houston.rr.com>写道:
Kirk,
Thanks for your input, hoever that is not exactly what I am trying to do.
I understand that q is local scope. I was trying to return q and make a
call to the function using another variable with global scope.

In other language
subroutine foo(b,c)
c=b*1000
return
call foo(q,r)
where q and r are defines and same type as b,c as function
How do I do this in python. I need to perform operations on a variable and pass the new variable to the program.
Hope this might clear it up.

Doug
"Kirk Strauser" <ki**@strauser.com> wrote in message
news:87************@strauser.com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2004-06-16T22:46:42Z, "Doug Jordan" <dj******@houston.rr.com> writes:
#function假设返回变量
def fctn2(c):
for c in c:
q = h * 80
def prntfctn(y):
对于j的y:
打印j

fctn2(列表)
prntfctn (q)
#function suppose to return variable
def fctn2(c):
for h in c:
q=h*80
return q

def prntfctn(y):
for j in y:
print j

fctn2(list)
prntfctn(q)



名称q仅存在于fctn2变量的范围内。如果你想让它出现在全球范围内,请在那里分配:

q = fctn2(list)
prtnfctn(q)

那应该做你想做的。请注意,我不知道任何现代的编程语言,它允许函数在没有明确请求的情况下为全局变量赋值。如果存在这样的事情,那么我强烈建议您不惜一切代价避免它。
- -
Kirk Strauser
Strauser Group
Open。解决方案。简单。
http://www.strausergroup.com/
-----开始PGP签名-----
版本:GnuPG v1.2.4(GNU / Linux)
iD8DBQFA0NS95sRg + Y0CpvERAlGRAKCkUJTaBJIckaWCvM2qkE mA8BDSEgCaAgcp
u44PX2uPlSMGYAV4VG5jaC8 =
= G3qn
-----结束PGP签名-----



The name "q" only exists inside the scope of the fctn2 variable. If you
want it present inside the global scope, assign it there:

q = fctn2(list)
prtnfctn(q)

That should do what you want. Note that I''m unaware of any modern
programming language that would allow a function to assign a value to a
global variable without explicitly requesting it. If such a thing exists,
then I highly recommend you avoid it at all costs.
- --
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA0NS95sRg+Y0CpvERAlGRAKCkUJTaBJIckaWCvM2qkE mA8BDSEgCaAgcp
u44PX2uPlSMGYAV4VG5jaC8=
=G3qn
-----END PGP SIGNATURE-----



这篇关于无法从函数传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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