Keep the lifespan of variable after multiple function calls?

后端 未结 4 1175
抹茶落季
抹茶落季 2021-01-27 17:18

Assuming:

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

is there a keyword to stop a variable(my_list) from being reassigned? Let\'s supp

相关标签:
4条回答
  • 2021-01-27 17:59

    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.

    0 讨论(0)
  • 2021-01-27 18:03

    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
    
    0 讨论(0)
  • 2021-01-27 18:17

    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

    0 讨论(0)
  • 2021-01-27 18:22

    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.

    0 讨论(0)
提交回复
热议问题