문제

Is it possible to use a list created in one function in a separate function?

I've created a list of discs in this function:

def create_discs():

    disc_list=['disc0', 'disc1', 'disc2']

I would then like to use this list in a different function

def move_discs

    cmds.move(disc_list[1], 0, 0, 5)

I get this error when trying to do so:

NameError: file <maya console> line 48: global name 'disc_list' is not defined #

I'm using Autodesk Maya

도움이 되었습니까?

해결책 2

If you don't want to use Classes to maintain state, then you may want to have a global variable or you can return the list from create_list().

Using global variable:

disc_list = []

def create_list():
    global disc_list

    disc_list = ['disc0', 'disc1', 'disc2']

def move_discs
    cmds.move(disc_list[1], 0, 0, 5)

Note that using global variables is not encouraged.

Returning list from create_list():

def create_list():
    return ['disc0', 'disc1', 'disc2']

def move_discs():
    disc_list = create_list()
    cmds.move(disc_list[1], 0, 0, 5)

But I don't think this will of much help to you as you probably need to keep the state of your list throughout the program.

다른 팁

You just need to return that list in your first function:

>>> def create_discs():
    disc_list=['disc0', 'disc1', 'disc2']
    return disc_list


>>> def move_discs():
    l = create_discs()
    print(l)
>>> move_discs()
['disc0', 'disc1', 'disc2']

you can make the first function return the wanted list and then use it anywhere you like:

def create_discs():
    disc_list = ['disc0', 'disc1', 'disc2']
    return disc_list

def move_discs():
    disc_list = create_discs()
    cmds.move(disc_list[1], 0, 0, 5)

hope this helps.

There are a couple ways to do what you're trying to do.

If you want to merely create the list using create_discs once in order for it be used in move_discs, you can modify create_discs to return the list it creates, and then call create_discs within move_discs:

def create_discs():
    return ['disc0', 'disc1', 'disc2']

def move_discs():
    cmds.move(create_discs()[1], 0, 0, 5)

move_discs()

If you want to have the created list saved so that it can be used throughout the program, you can again have create_discs return its list, and have it save to a variable. This variable can then be accessed by moved_discs or any other function:

def create_discs():
    return ['disc0', 'disc1', 'disc2']

def move_discs():
    cmds.move(saved_list[1], 0, 0, 5)

saved_list = create_discs()

I'm not sure if you have researched function arguments yet, but they are a feature of functions which support the very behavior you're trying to achieve:

def create_discs():
    return ['disc0', 'disc1', 'disc2']

def move_discs(list):
    print(list[1])

saved_list = create_discs()
move_discs(saved_list)  # This will output 'disc1'

saved_list = ['new0', 'new1', 'new2']
moved_discs(saved_list)  # This will now output 'new1'

Lists are persistent objects on their own. If you return the list from one function you can manipulate it in others:

def get_list():
    return [1,2,3,4]


def remove_last_item( the_list):
    the_list.remove(the_list[-1]) #  remove the last entry


def insert_something(the_list, item):
    the_list.insert(item, 0)

my_list = get_list()
print my_list
# [1,2,3,4]

remove_last_item(my_list)
print my_list
# [1,2,3]

insert_something(my_list, 0)
print my_list
# [0,1,2,3]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top