-
Notifications
You must be signed in to change notification settings - Fork 0
Variables
In Python all variables point to objects. These objects have 3 components: id, value and type.
For example, when we execute this line foo = 420
, internally python creates a new object with a unique id, value of 420 and type of Type:Integer. The variable foo
will be pointing to that object.
As all variables point to an object, their type can be reassigned. By atrributing our variable to a string foo = "IEEE"
, python will create a new object with a new ID, value of "IEEE" and type of Type:String.
To summarize, the type of a variable is only attached to the object it points to. This allows you to change its type whenever you want/need to. As the variables are light (they only point to the object), reusing the same variable for different types doesn't result in a significant memory improvement. However, this can lead to confusing code, so it's good practice to keep the types of your variables consistent.
The explanation for the reassigned objects being deleted is that every object has a reference count, the number of times it is referenced. This number is always positive and once it reaches zero, the garbage collector is called to free the memory used by that object, efectively making it disappear. If you ever wish to know this reference count use the following code:
>>> import sys
>>> name = "IEEE"
>>> sys.getrefcount(name)
2
Please note that, as the documentation says, the reference count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument to getrefcount().
Python enforces that a variable should never have the name of a keyword or start with numbers, doing so will result in a syntax error:
>>> if = "if"
File "<stdin>", line 1
if = "if"
^
SyntaxError: invalid syntax
Note: For a list of all keywords that will produce a syntax error:
>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Other naming conventions that are not mandatory, but are considered good practice:
- Separate words in variable names either with underscores (big_variable) or with capital letters (bigVariable). Use only one of these throughout your code.
- Don't override built-ins.
Although built-in words could be used for a variable, doing this will not allow you to access them again and is considered a bad practice. The only way to use that built-in word again is through the __builtins__
module.
Some of these words can be used in accident, such as dict, id, list, min, max or str.
Note: For a list of all built-ins that you should avoid:
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
Previous: Interpreter
Next: Data Types