0

How to explicitly declare a list as a local variable that cannot be touched from anywhere except the function its declared in?

tried

LOCAL variable = []

doesnt work

Psidom
  • 209,562
  • 33
  • 339
  • 356
mysan
  • 3
  • 4

1 Answers1

0

Python is the interpreted language and it hasn't such statements.

Here is from the documentation:

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice

You can notice such _pseudo_private_var(one underscore) naming.It is just a convention, it just tells you SHOUDN'T touch this variable but you CAN change value of this one at the same time. It is a python). There is __dynamic_obj_relative_naming as well(two underscores).

More about scopes and namespaces rules you could find here Classes, namespace and scope rules. There are few interesting statements you are interested in probably global and nonlocal. They are like opposites of yours.

kvdm.dev
  • 141
  • 1
  • 1
  • 12