Keep the lifespan of variable after multiple funct

2019-03-03 07:51发布

问题:

Assuming:

def myfunc(x):
    my_list  = []
    list.append(x)

is there a keyword to stop a variable(my_list) from being reassigned? Let's suppose that NA is the keyword:

def myfunc(x):
    NA listv  = []
    list.append(x)

in such way that the line will be read and not reassigned but the variable still active appending new values for every function call. I know about the GLOBAL variable, but I just want to know is the keyword exists!

回答1:

You can declare the variable globally as in C/C++ and use global keyword in python to refer them as variable in global data section. Some of references about them:

  • https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

  • http://www.geeksforgeeks.org/global-local-variables-python/

  • Use of "global" keyword in Python



回答2:

I guess you are looking for something equivalent to the final keyword in Java. Simply put, there is no such keyword. The Python attitude is "if you don't want this modified at all, just don't modify it, I wont stop you though".

However you can mimic something like it by some tweaks in your class.

class MyFinalVariablesClass:
    def __setattr__(self, attr, value):
        # if it already has this attribute, complain !
        if hasattr(self, attr):
            raise Exception("Attempting to alter read-only value")

        self.__dict__[attr] = value

Or in the case of your variable (haven't tested it)

class WriteOnceReadWhenever:
    def __setattr__(self, attr, value):
        if attr == 'my_list' and hasattr(self, attr):
            raise Exception("Attempting to alter read-only value")
        self.__dict__[attr] = value


回答3:

I believe the behaviour you're seeking is almost exactly how mutable default arguments work in python: instantiated only during function definition.

def myfunc(x,*args,my_list=[]):
    my_list.append(x)

The *args will protect the list from being overwritten by an erroneously passed second argument.



回答4:

There is not a keyword to do this. There are workarounds (such as the mutable default argument), but there are better ways to achieve the same thing.

For exaple you could set the variable as a function attribute:

def my_func():
    my_func.my_list.append(1)

my_func.my_list = []

However this is just a hacky way of doing this:

class MyFuncFactory:
    def __init__(self):
        self.my_list = []
    def __call__(self):
        self.my_list.append(1)

Hence you are probably better of writing a class instead of a function in this case.


Sometimes you could use a nonlocal variable. For example to define a key function which requires state I usually write something like:

def my_key_builder():
    some_state = 0

    def key_function(arg):
        nonlocal some_state
        some_state += 1
        return some_state % 2
    return key_function

for x in sorted(a_list, key=my_key_builder()):
    print(x)

Using a nonlocal instead of a global (or mutable default argument) allows you to use the key function multiple times. However if the function becomes slightly bigger using a class is probably the best option.



标签: python scope