ФорумПрограммированиеPython → Помогите с тестовым заданием

Помогите с тестовым заданием

  • polin11

    Сообщения: 41 Репутация: N Группа: Кто попало

    Spritz 8 апреля 2018 г. 19:14

    Выскажите ваше мнение, у меня нет предположений -(
    Q: how do you make a variables only accessible within a function but cause the value to persist
    across calls to that function
    Answer:
    1) Add a positional argument, with an empty tuple as default value, to the end of the argument list
    2) Encapsulate access to the variable within set_x() and get_y() functions
    3) Create a global variable at the veginning of the module and add the private declaration to that variable within the function
    4) Create a global variable at the beginning of the module and add the "static" declaration to that variable within the function
    5) Add the keyword argument, with a mutable a default value to the end of the function parameret list

  • Sinkler

    Сообщения: 7958 Репутация: N Группа: в ухо

    Spritz 9 апреля 2018 г. 11:18, спустя 16 часов 4 минуты 35 секунд

    @polin11, пятое сработает

    In [1]: def foo(mutable={'baz': 0}):
       ...:     mutable['baz'] += 1
       ...:     return mutable['baz']
       ...:     
    
    In [2]: foo()
    Out[2]: 1
    
    In [3]: foo()
    Out[3]: 2
    
    In [4]: foo()
    Out[4]: 3
    

    но mutable default value это плохой паттерн считается

    в питоне нет настоящих приватных переменных и это норм, а если нужна статическая, то просто кладёшь её в модуль и изменяешь или юзаешь класс или ещё есть методы Static variable in Python? [stackoverflow.com]

Пожалуйста, авторизуйтесь, чтобы написать комментарий!