Skip to content

Commit 2a0968c

Browse files
committed
Update structure.rst
1 parent a59c581 commit 2a0968c

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

docs/writing/structure.rst

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,34 @@ The dynamic typing of Python is often considered to be a weakness, and indeed
326326
it can lead to complexities and hard-to-debug code. Something
327327
named 'a' can be set to many different things, and the developer or the
328328
maintainer needs to track this name in the code to make sure it has not
329-
been set to a completely unrelated object.
330-
329+
been set to a completely unrelated object. But sometimes it will lead to many
330+
complexities, for example, look at the following code:
331+
.. code-block:: python
332+
def compress(obj):
333+
if type(obj) == str:
334+
return compressed_string(obj)
335+
elif type(obj) == int:
336+
return compressed_int(obj)
337+
elif type(obj) == list:
338+
return [compress(i) for i in obj]
339+
It could be used for when a user enters something that the program doesn't know about,
340+
then they would have to do this:
341+
.. code-block:: python
342+
def compress(s=None,i=None,l=None):
343+
assert s or i or l
344+
if s:
345+
return compressed_string(obj)
346+
elif i:
347+
return compressed_int(obj)
348+
elif l:
349+
return [compress(i) for i in obj]
350+
try:
351+
compress(s=some_obj)
352+
except TypeError:
353+
try:
354+
compress(i=some_obj)
355+
except TypeError:
356+
compress(l=some_obj)
331357
Some guidelines help to avoid this issue:
332358

333359
- Avoid using the same variable name for different things.

0 commit comments

Comments
 (0)