mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
bpo-40341: Remove some "discouraged solutions" in Doc/faq/programming.rst (GH-22726) (GH-22727)
(cherry picked from commit a22a19f354
)
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
This commit is contained in:
parent
dff9161821
commit
0fbddb14dc
1 changed files with 1 additions and 21 deletions
|
@ -942,7 +942,7 @@ There are various techniques.
|
||||||
f()
|
f()
|
||||||
|
|
||||||
|
|
||||||
* Use :func:`locals` or :func:`eval` to resolve the function name::
|
* Use :func:`locals` to resolve the function name::
|
||||||
|
|
||||||
def myFunc():
|
def myFunc():
|
||||||
print("hello")
|
print("hello")
|
||||||
|
@ -952,12 +952,6 @@ There are various techniques.
|
||||||
f = locals()[fname]
|
f = locals()[fname]
|
||||||
f()
|
f()
|
||||||
|
|
||||||
f = eval(fname)
|
|
||||||
f()
|
|
||||||
|
|
||||||
Note: Using :func:`eval` is slow and dangerous. If you don't have absolute
|
|
||||||
control over the contents of the string, someone could pass a string that
|
|
||||||
resulted in an arbitrary function being executed.
|
|
||||||
|
|
||||||
Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
|
Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
|
||||||
-------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------
|
||||||
|
@ -1381,20 +1375,6 @@ out the element you want. ::
|
||||||
['else', 'sort', 'to', 'something']
|
['else', 'sort', 'to', 'something']
|
||||||
|
|
||||||
|
|
||||||
An alternative for the last step is::
|
|
||||||
|
|
||||||
>>> result = []
|
|
||||||
>>> for p in pairs: result.append(p[1])
|
|
||||||
|
|
||||||
If you find this more legible, you might prefer to use this instead of the final
|
|
||||||
list comprehension. However, it is almost twice as slow for long lists. Why?
|
|
||||||
First, the ``append()`` operation has to reallocate memory, and while it uses
|
|
||||||
some tricks to avoid doing that each time, it still has to do it occasionally,
|
|
||||||
and that costs quite a bit. Second, the expression "result.append" requires an
|
|
||||||
extra attribute lookup, and third, there's a speed reduction from having to make
|
|
||||||
all those function calls.
|
|
||||||
|
|
||||||
|
|
||||||
Objects
|
Objects
|
||||||
=======
|
=======
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue