<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>python 3 Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/python-3/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/python-3/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Tue, 23 Dec 2025 08:39:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Python3: ChainMap</title>
		<link>https://creatronix.de/python3-chainmap/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 23 Nov 2018 13:33:39 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[chainmap]]></category>
		<category><![CDATA[python 3]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=2440</guid>

					<description><![CDATA[<p>Since Python 3.3 You can chain dictionaries which contain the same key in a prioritized order: from collections import ChainMap prio_1 = {"param_1": "foo"} prio_2 = {"param_1": "foobar", "param_2": "bar"} combined = ChainMap(prio_1, prio_2) print(combined["param_1"]) # outputs 'foo' print(combined["param_2"]) # outputs 'bar' The param_1 from the prio_1 dictionary is dominant, so it isn&#8217;t overwritten by&#8230;</p>
<p>The post <a href="https://creatronix.de/python3-chainmap/">Python3: ChainMap</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Since Python 3.3 You can chain dictionaries which contain the same key in a prioritized order:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>from collections import ChainMap 

prio_1 = {"param_1": "foo"} 
prio_2 = {"param_1": "foobar", "param_2": "bar"} 

combined = ChainMap(prio_1, prio_2) 

print(combined["param_1"]) # outputs 'foo' 
print(combined["param_2"]) # outputs 'bar'</code></pre>
</div>
<p>The param_1 from the prio_1 dictionary is dominant, so it isn&#8217;t overwritten by the key-value pair from prio_2 dict. prio_2 dict just adds the param_2 because it is not yet in the ChainMap.</p>
<p>The nice thing is that no information is lost, the ChainMap object contains both dictionaries.</p>
<p>You can use it for example in a situation where you want to chain arguments from argparse, os.environ and default parameters. <a href="https://docs.python.org/3/library/collections.html#chainmap-examples-and-recipes">See here</a></p>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/python3-chainmap/">Python3: ChainMap</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python Type Checking</title>
		<link>https://creatronix.de/python-type-checking/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Thu, 26 Apr 2018 09:31:55 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[type checking]]></category>
		<category><![CDATA[type hints]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=1511</guid>

					<description><![CDATA[<p>Python is a dynamically typed language which makes it easy and fun to program. But sometimes -especially in bigger projects- it can become quite cumbersome when you just receive errors at run time. Given the hypothetical example where we define a function which multiplies integer: def multiply(a, b): return a * b print(multiply("I", "You")) It&#8230;</p>
<p>The post <a href="https://creatronix.de/python-type-checking/">Python Type Checking</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Python is a dynamically typed language which makes it easy and fun to program. But sometimes -especially in bigger projects- it can become quite cumbersome when you just receive errors at run time.</p>
<p>Given the hypothetical example where we define a function which multiplies integer:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>def multiply(a, b): 
    return a * b 

print(multiply("I", "You"))</code></pre>
</div>
<p>It is possible to pass strings to the function which will produce am error at runtime</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>TypeError: can't multiply sequence by non-int of type 'str'</code></pre>
</div>
<h2>Type Hints</h2>
<p>Since Python 3.5 you have the possibility to add type hints so that type checks can be performed _before_ run time:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>def multiply(a: int, b: int) -&gt; int: 
    return a * b</code></pre>
</div>
<p>You can also have type hints with default parameter values. They just look a bit weird 🙂</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>def __init__(self, wp_url: str, posts_per_page: int=20): 
    self.wp_api_url = wp_url + "/wp-json/wp/v2" 
    self.posts_per_page = posts_per_page</code></pre>
</div>
<p>You can import more types from the typing module e.g. List</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>from typing import List 

def get_categories(self) -&gt; List: 
    query_string = "/categories?per_page=20" 
    categories = requests.get(self.wp_api_url + query_string) 
    return categories.json()</code></pre>
</div>
<h2>Union</h2>
<p>If a function returns different object types we can use the union type</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>from typing import Union, Optional 

def get_foo_or_bar(id: int) -&gt; Union[Foo, Bar]
    pass
def get_foo_or_none(id: int) -&gt; Union[Foo, None]
    pass
def get_foo_or_none(id: int) -&gt; Optional[Foo]
    pass</code></pre>
</div>
<h2>Type Checking</h2>
<p>With <a href="http://www.mypy-lang.org/">mypy</a> there is tool to lint your python code before execution:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>$ python3 pip install mypy 
$ mypy &lt;my_python_file&gt;</code></pre>
</div>
<h2>IDE support</h2>
<p>An IDE like PyCharm is already capable of checking types with type hints</p>
<p><img decoding="async" class="alignnone size-full wp-image-1512" src="https://creatronix.de/wp-content/uploads/2018/04/type_checking.png" alt="" width="291" height="152" /></p>
<p><a href="https://medium.com/@ageitgey/learn-how-to-use-static-type-checking-in-python-3-6-in-10-minutes-12c86d72677b">A good tutorial on medium</a></p>
<p><iframe title="Carl Meyer - Type-checked Python in the real world - PyCon 2018" width="1200" height="675" src="https://www.youtube.com/embed/pMgmKJyWKn8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></p>
<p>The post <a href="https://creatronix.de/python-type-checking/">Python Type Checking</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python 3  &#8211; there shall be just int</title>
		<link>https://creatronix.de/python-3-there-shall-be-just-int/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Mon, 14 Aug 2017 14:54:47 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[long]]></category>
		<category><![CDATA[python 2]]></category>
		<category><![CDATA[python 3]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=984</guid>

					<description><![CDATA[<p>Trying to contribute to the Flask plugin flask-login I just added these lines: if isinstance(duration, (int, long)): duration = timedelta(seconds=duration) Looking quite plausible, isn&#8217;t it? But lo and behold: it doesn&#8217;t work under Python 3.x. Dang! The reason: Python 2 has two integer types: int and long. In Python 3 there is only int, which&#8230;</p>
<p>The post <a href="https://creatronix.de/python-3-there-shall-be-just-int/">Python 3  &#8211; there shall be just int</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Trying to contribute to the Flask plugin <a href="https://github.com/maxcountryman/flask-login">flask-login</a> I just added these lines:</p>
<pre>if isinstance(duration, (int, long)):
    duration = timedelta(seconds=duration)</pre>
<p>Looking quite plausible, isn&#8217;t it? But lo and behold: it doesn&#8217;t work under Python 3.x. Dang!</p>
<p>The reason: Python 2 has two integer types: int and long. In Python 3 there is only int, which makes it necessary to distinguish between these two major versions. I&#8217;ve found a <a href="http://python3porting.com/differences.html">nice page</a> which deals with this issue. Here is what You must do to make it work in both Python 2 and 3:</p>
<pre><span class="kn">import</span> <span class="nn">sys</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">3</span><span class="p">,):</span>
    i<span class="n">nteger_types</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">long</span><span class="p">,)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">integer_types</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">,)</span>

<span class="nb">isinstance</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">integer_types</span><span class="p">)</span></pre>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/python-3-there-shall-be-just-int/">Python 3  &#8211; there shall be just int</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
