Inheritance [RIP] .1 Applicability to language Python supports inheritance through a hierarchical search of namespaces starting at the subclass and proceeding upward through the superclasses. Multiple inheritance is also supported. Any inherited methods are subject to the same vulnerabilities that occur whenever using code that is not well understood. .2 Guidance to language users Inherit only from trusted classes; and Use Python’s built-in documentation (such as docstrings) to obtain information about a class’ method before inheriting from it. Extra Intrinsics [LRM] .1 Applicability to language Python provides a set of built-in intrinsics which are implicitly imported into all Python scripts. Any of the variables and functions can therefore easily be overridden: x = 'abc' print(len(x))#=> 3 def len(x): return 10 print(len(x))#=> 10 If the example the built-in function is overridden with logic that always returns Note that the len 10. def statement is executed dynamically so the new overriding function has not yet been defined when the first len call to is made therefore the built-in version of is called in line 2 and it returns the expected result (3 len len in this case). After the new function is defined it overrides all references to the builtin-in function in len len the script. This can later be by explicitly importing the built-in function with the following code: len “undone” from builtins import len print(len(x))#=> 3 It’s very important to be of name resolution rules when overriding built-ins (or anything else for that matter). In the example below, the overriding function is defined within another function and therefore is len not found using the LEGB rule for name resolution (see ): x = 'abc' print(len(x))#=> 3 def f(x): def len(x): return 10 print(len(x))#=> 3
Inheritance [RIP] .1 Applicability to language Python supports inheritance through a hierarchical search of namespaces starting at the subclass and proceeding upward through the superclasses. Multiple inheritance is also supported. Any inherited methods are subject to the same vulnerabilities that occur whenever using code that is not well understood. .2 Guidance to language users Inherit only from trusted classes; and Use Python’s built-in documentation (such as docstrings) to obtain information about a class’ method before inheriting from it. Extra Intrinsics [LRM] .1 Applicability to language Python provides a set of built-in intrinsics which are implicitly imported into all Python scripts. Any of the variables and functions can therefore easily be overridden: x = 'abc' print(len(x))#=> 3 def len(x): return 10 print(len(x))#=> 10 If the example the built-in len function is overridden with logic that always returns 10. Note that the def statement is executed dynamically so the new overriding len function has not yet been defined when the first call to len is made therefore the built-in version of len is called in line 2 and it returns the expected result (3 in this case). After the new len function is defined it overrides all references to the builtin-in len function in the script. This can later be “undone” by explicitly importing the built-in len function with the following code: from builtins import len print(len(x))#=> 3 It’s very important to be of name resolution rules when overriding built-ins (or anything else for that matter). In the example below, the overriding len function is defined within another function and therefore is not found using the LEGB rule for name resolution (see ): x = 'abc' print(len(x))#=> 3 def f(x): def len(x): return 10 print(len(x))#=> 3
ISO-IECJTC1-SC22-WG23_N0541-tr24772-4-draft-2015-05-26.docx