开发者

How to call a Python function like in Javascript

开发者 https://www.devze.com 2023-03-04 03:16 出处:网络
if i do this , it will be ok : def a(): b = [1,2,3] def d(): print b d() a() but i have many methodneed function d , so i have to defined it out of the function a , but it show error :

if i do this , it will be ok :

def a():
    b = [1,2,3]
    def d():
        print b
    d()

a()

but i have many method need function d , so i have to defined it out of the function a , but it show error :

def d():
    print b
def a():
    b = [1,2,3开发者_如何学Go]
    d()

a()

error:

  File "c.py", line 21, in <module>
    a()
  File "c.py", line 19, in a
    d()
  File "c.py", line 16, in d
    print b
NameError: global name 'b' is not defined

so i have many different Variable, like b in different functions , so i don't want to send Variable like this :

def d(b1,b2,b3,b4,b5):
    print b1,b2,b3,b4,b5

that is not very simple , so i do it like this :

d = \
'''
def d():\n
    print b
'''
def a():
    b = [1,2,3]
    c = eval(d)
    c()

a() 

but it also show error:

  File "c.py", line 16, in <module>
    a()
  File "c.py", line 13, in a
    c = eval(d)
  File "<string>", line 2
    def d():
      ^

so what can i do ,

thanks


You are probably getting confused by javascript's scoping which is the opposite of python's. Read what the official Python tutorial has to say about scopes and namespaces before you go any further! You might also find this talk by Ian Bicking useful: Javascript for People Who Know Python. He does a great job covering the differences between the two languages.

In this case, you are trying to access an object with a name that is defined in another function's namespace, which means it has no way to know about it. The ideal thing to do would be to rewrite d() so it accepts an argument and pass it b. You could also declare b as a global using global, but you should avoid that.

def d():
    print b # b is defined in a so it's only available in a's local namespace
def a():
    b = [1,2,3] 
    d() # if you rewrite d to accept an argument, you can pass it b and this will work

Do this:

def d(b): # accepts an argument now
    print b

def a():
    b = [1,2,3]
    d(b) # calls d with an argument of b

If you are curious what names are in the various scopes you can do this:

import __builtin__
def d():
    print dir(__builtin__) # built-in names
    print dir(globals()) # global names
    print dir(locals()) # local names, defined in d 

I think you'll find Python's namespaces/scoping to be fairly intuitive.


It looks like you want to make a into a class:

class A(object):
    b = [1,2,3]
    def d():
        print b

A.d()
A.b = [4, 5, 6]
A.d()


To share variables you can use objects. Define some class which you will use as data container.


Without seeing the JavaScript you're trying to emulate, I can only guess.

But why don't you just pass b to d:

def d(b):
    print b
def a():
    b = [1,2,3]
    d(b)

a()

Or if that's not an option, maybe you could declare b as global:

b = []
def d():
    print b
def a():
    global b
    b = [1,2,3]
    d()

a()

Or maybe you're writing a wrapper to always do something before calling each function. In that case, decorators could help:

def test(b):
    def t(f):
        def d():
            print b
            return f(b)
        return d
    return t

@test([1,2,3])
def a(b):
    pass

a()


Just to amend the existing good answers by Mikel and zeekay, here is the difference in JavaScript. What you probably had was:

function d() {
  print(b);
}

function a() {
  b = [1,2,3];
  d();
}

But what you get in Python is the equivalent of this:

function d() {
  print(b);
}

function a() {
  var b = [1,2,3];
  d();
}

(Mind the var b in the second version). If you know JavaScript you get the difference. Argument passing is, as suggested, the preferable way. Using the global key word would be another (less preferable) way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号