Python definition function parameter passing [closed]

柔情痞子 提交于 2021-01-29 13:41:54

问题


For most of you this is probably common sense but I am finding it very, very hard to get a handle on this. If you create a function such as

def one():
    x=1
    y=2
    z=3
    h=33
    j=454
    l=353
    m=898

then lets say I have another function, def two():, and I want to use the variables in def one() into def two, and I also create two new variables in def two(). How would I go about doing that.

Then, lets say that I have a third function def three(), how would I use the variables in def 1 and def 2 into def 3? The methods I am using are very very inefficient and probably doing more than is required. What I do is in def two, a add all the variables from def one into def two's parameters, and then I also call function one in the def of function 2. I know there are numerous threads on this on SO, and I have looked through many of them and they don't simply it. They use very specific and advanced code that I am unfamiliar with and therefore don't understand. My example is very straightforward and simple and I'm sure if answered it will help many newcomers.Thanks!

EDIT:I don't want a "simple" way that ends up using advanced code I am not familiar with. I want an "efficient" method that is simple to understand.


回答1:


If I understand what do you need then I think simplest way that python brings you is next ->

class Helper:pass 

# use name space
def one(a):  
    a.x=1
    a.y=2
    a.z=3
    a.h=33
    a.j=454
    a.l=353
    a.m=898

def two(a):
    a.y=7

a=Helper()

one(a)
print(a.x,a.y)
two(a)
print(a.x,a.y)

#-------------------------------------------------------------------
# I think you have solution and dont need to read next lines :)
# next lines are just bonus ;)
#-------------------------------------------------------------------

# you could use more independent "namespaces"
b=Helper()
one(b)
print(b.y)
print(a.y)  # "namespace a" is unchanged

# if you like to see which attributes are defined you could use funciton dir
# (under python3 it is a little more complicated)
print(dir(a))
print(dir(b))

# please dont do next lines at home!!! :P:P:P (__builtins__ is special you could get unwanted results!) 
one(__builtins__)

print(x) # 



回答2:


I think the best solution would be adopting an OO paradigm for your code (classes, methods and inheritance).

Using simple functions, you can just choose between using global variables and local scoped.




回答3:


Data is passed into functions as arguments, and out of functions as return values. The ideal case for a function is when you have no need to access variables out of the scope of the function.

If you have a lot of numbers, perhaps you should declare them as a list (easier to manage) and then pass them into functions as arguments and out of functions, as the transmuted results, as a return value?




回答4:


You can use the global keyword to create and modify global variables like this:

In [1]: def one():
   ...:     global a
   ...:     a = 1
   ...: 

In [2]: one()

In [3]: a
Out[3]: 1

However, having that many (or maybe even any) global variables is not considered a good practice. You can declare one global list and keep all those values in it, that would be cleaner; although you should try your best to stick with using function arguments and return values as outlined in Andrew's answer.




回答5:


Without being at all clear on what you're trying to do, here's a simple way you can do this:

class One(object):
    x=1
    y=2
    z=3
    h=33
    j=454
    l=353
    m=898


class Two(One):
    n=42
    o=1337

print Two.x, Two.n  # 1, 42


来源:https://stackoverflow.com/questions/13172511/python-definition-function-parameter-passing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!