<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Julien AWON'GA</title>
    <description>The latest articles on Forem by Julien AWON'GA (@julienawonga).</description>
    <link>https://forem.com/julienawonga</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1246061%2F921117c1-991f-41db-b740-ea6b26ab2b7d.jpeg</url>
      <title>Forem: Julien AWON'GA</title>
      <link>https://forem.com/julienawonga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/julienawonga"/>
    <language>en</language>
    <item>
      <title>Ellipsis in Python</title>
      <dc:creator>Julien AWON'GA</dc:creator>
      <pubDate>Fri, 05 Jan 2024 14:00:00 +0000</pubDate>
      <link>https://forem.com/julienawonga/ellipsis-in-python-fbd</link>
      <guid>https://forem.com/julienawonga/ellipsis-in-python-fbd</guid>
      <description>&lt;p&gt;As a Python developer, you may have come across the ellipsis (...) symbol and wondered what it means. In Python, an ellipsis is a built-in constant that represents a placeholder for code that you will add later. This constant is used in a variety of ways in Python, and in this article, we'll explore some of the common use cases of ellipsis.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Ellipsis as a Placeholder
&lt;/h2&gt;

&lt;p&gt;In Python, you can use the ellipsis constant as a placeholder for code that you haven't written yet. This is useful when you are working on a complex program and need to come back to a particular section later. You can use the ellipsis to mark the section that you need to revisit, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;complex_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# TODO: implement this function
&lt;/span&gt;    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using Ellipsis in Slicing
&lt;/h2&gt;

&lt;p&gt;In Python, you can use ellipsis in slicing to represent an unspecified number of dimensions. When used in a slice, the ellipsis symbol means "all remaining dimensions." This is particularly useful when working with multidimensional arrays.&lt;/p&gt;

&lt;p&gt;For example, consider a 3D numpy array arr with shape (3, 4, 5). To select all elements in the first dimension, you can use the : symbol:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[:,&lt;/span&gt; &lt;span class="p"&gt;:,&lt;/span&gt; &lt;span class="p"&gt;:]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To select all elements in the second dimension, you can use the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr[:, :, :]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if you want to select all elements in the third dimension? Here's where ellipsis comes in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr[..., :]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the ellipsis symbol represents the unspecified dimensions. The resulting slice will select all elements in the third dimension.&lt;/p&gt;

&lt;p&gt;Using Ellipsis in Function Arguments&lt;br&gt;
In Python, you can use the ellipsis symbol in function arguments to indicate that the argument is optional. This is often used in function definitions where the function can accept an arbitrary number of arguments.&lt;/p&gt;

&lt;p&gt;For example, consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the *args and **kwargs arguments indicate that the function can accept an arbitrary number of positional and keyword arguments. If you want to indicate that a particular argument is optional, you can use the ellipsis symbol as its default value, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;...):&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the arg3 argument is optional, and its default value is ....&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Ellipsis in Type Annotations
&lt;/h2&gt;

&lt;p&gt;In Python, you can also use ellipsis in type annotations to represent a type that you haven't defined yet. This is useful when you're working on a large codebase and need to define types incrementally.&lt;/p&gt;

&lt;p&gt;For example, consider the following type annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the arg3 parameter has an ellipsis type annotation, indicating that its type is unspecified. When you come back to this function later, you can fill in the type annotation for arg3.&lt;/p&gt;

&lt;p&gt;While the ellipsis constant may seem like a small and obscure feature in Python, it can actually be quite useful in certain situations. By using ellipsis as a placeholder for code that you haven't written yet, you can write more flexible and efficient Python code that is easier to maintain over time.&lt;/p&gt;

&lt;p&gt;I hope this article has helped you understand what ellipsis is and how it can be used in Python. If you have any questions or comments, feel free to leave them below!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Walrus Operator in Python</title>
      <dc:creator>Julien AWON'GA</dc:creator>
      <pubDate>Tue, 02 Jan 2024 09:30:00 +0000</pubDate>
      <link>https://forem.com/julienawonga/walrus-operator-in-python-42a3</link>
      <guid>https://forem.com/julienawonga/walrus-operator-in-python-42a3</guid>
      <description>&lt;h2&gt;
  
  
  What is the Walrus Operator?
&lt;/h2&gt;

&lt;p&gt;The Walrus Operator, also known as the Assignment Expression, is a new operator in Python 3.8 that allows you to assign a value to a variable as part of an expression. The syntax of the operator is :=, and it can be used in any expression where you would normally use an assignment statement.&lt;/p&gt;

&lt;p&gt;For example, let's say you want to read a line from a file and print it only if the line contains the word "Python". In pre-3.8 Python, you would write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;file.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Python&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the Walrus Operator, you can simplify this code by combining the readline() method and the if statement into a single expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;file.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readline&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Python&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the Walrus Operator assigns the result of f.readline() to the variable line and also checks if line contains the word "Python". If both conditions are true, the line is printed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use the Walrus Operator
&lt;/h2&gt;

&lt;p&gt;The Walrus Operator can be used in any expression that requires an assignment statement. Some common use cases include:&lt;/p&gt;

&lt;p&gt;Loop conditions: You can use the Walrus Operator to simplify loop conditions. For example, instead of writing while True: and then checking for a condition inside the loop, you can write while (&lt;code&gt;x := get_next_value()&lt;/code&gt;): and assign the next value to x within the condition.&lt;/p&gt;

&lt;p&gt;List comprehensions: You can use the Walrus Operator in list comprehensions to simplify the code. For example, instead of writing &lt;code&gt;[x for x in some_list if some_condition(x)]&lt;/code&gt;, you can write &lt;code&gt;[x for x in some_list if (y := some_condition(x))]&lt;/code&gt; and use y later in the comprehension.&lt;/p&gt;

&lt;p&gt;Function calls: You can use the Walrus Operator in function calls to simplify the code. For example, instead of writing &lt;code&gt;result = some_function()&lt;/code&gt; and then checking if result is not None, you can write if (&lt;code&gt;result := some_function()&lt;/code&gt;) is not None: and assign the result to result within the if statement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveats and Best Practices
&lt;/h2&gt;

&lt;p&gt;While the Walrus Operator can be very useful, there are some caveats and best practices that you should keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Don't overuse it: While the Walrus Operator can make your code more concise, overusing it can make your code less readable and harder to maintain. Use it only when it makes sense and improves the code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't use it in complex expressions: The Walrus Operator should only be used in simple expressions. Using it in complex expressions can make the code harder to read and understand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't use it for side effects: The Walrus Operator should only be used for assigning values to variables. Using it for side effects (e.g., opening a file) can make the code harder to read and understand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be aware of operator precedence: The Walrus Operator has a lower precedence than most other operators, so be careful when using it in expressions with other operators.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Walrus Operator is a useful addition to Python 3.8 that can simplify your code and make it more concise. It allows you to assign values to variables within expressions, which can be very handy in many situations. However, you should be careful not to overuse it and follow best practices when using it.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
