doing python wrong
This commit is contained in:
parent
2ce6be64e0
commit
39a2ff692a
99
_posts/2019-09-04-wrong-python.md
Normal file
99
_posts/2019-09-04-wrong-python.md
Normal file
@ -0,0 +1,99 @@
|
||||
---
|
||||
layout: post
|
||||
title: "Doing Python the wrong way"
|
||||
description: "In the name of science!"
|
||||
date: 2019-09-07 13:13:00
|
||||
categories: projects random
|
||||
---
|
||||
|
||||
If you know me, you probably know of the many weird things I do with python. Most recent of which being this [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) implementation in one line of python code:
|
||||
```python
|
||||
_=[print("FizzBuzz"[_*_%3*4:8--_**4%5] or _) for _ in range(101)]
|
||||
```
|
||||
|
||||
This installment of "weird things I do with python" will not focus on one-liners (that's going on my todo list though). But instead, playing with Python's classes and object system.
|
||||
|
||||
## A quick introduction to classes
|
||||
Im going to assume that you, the reader, have some reasonable knowledge of how computers work, and OOP concepts. If you do not, there are [many great online resources](https://medium.com/swlh/5-free-object-oriented-programming-online-courses-for-programmers-156afd0a3a73) to help you out.
|
||||
|
||||
As a quick refresher, this is the Python syntax for a basic class:
|
||||
```python
|
||||
class MyClass:
|
||||
|
||||
# This is the constructor. __init__ is an overridable python built-in
|
||||
def __init__(self, arg1: int):
|
||||
|
||||
# Here we set the class' scoped my_number to arg1
|
||||
self.my_number = arg1
|
||||
|
||||
def printMyNumber(self):
|
||||
print(self.my_number)
|
||||
```
|
||||
|
||||
This is really just a fancy setter and getter. Here is some example usage:
|
||||
```python
|
||||
my_object = MyClass(10)
|
||||
my_object.printMyNumber() # Prints 10
|
||||
```
|
||||
|
||||
## Noticing something odd
|
||||
Before reading the following, keep in mind that (as of now) I have not actually looked at the Python interpreter's source code enough to know about their memory system. The following is just an educated guess.
|
||||
|
||||
Looking at any python class, you may notice that **at least** 1 argument is required. `self` is used to access the class' data from itself. This is not present in most other languages I know, which means there might be something interesting happening behind the scenes. Here is a re-implementation of `MyClass` from above in java:
|
||||
```java
|
||||
public class MyClass {
|
||||
int my_int;
|
||||
|
||||
public MyClass(int arg1){
|
||||
my_int = arg1;
|
||||
}
|
||||
|
||||
public void printMyNumber(){
|
||||
System.out.println(my_int);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Notice the fact that there is no `self`? Yet Java methods can still access class data.
|
||||
|
||||
## Implementing objects in a non-object oriented language
|
||||
In a non-OOP language (like C), objects can be faked by creating [structures](https://en.wikipedia.org/wiki/Struct_(C_programming_language)) and some standard functions. These functions then take a pointer to their "parent" structure. Confusing? yes. But it works, and I see it used all over the place. Here a pseudocode example:
|
||||
```
|
||||
struct MyClass {
|
||||
int my_int; // Scpoed int
|
||||
}
|
||||
|
||||
fn printMyNumber(MyClass* self){
|
||||
print(self.my_int);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
`printMyNumber` takes a pointer to it's "parent class", called `self`. Look familiar? This is how Python works.
|
||||
|
||||
## Let's do some Python
|
||||
Alright.. Time for some "broken" Python. Here is yet another implementation of `MyClass`, except this time, each function is globally scoped:
|
||||
```python
|
||||
|
||||
# Private, globally scoped functions
|
||||
def _init_myclass(self, arg1: int):
|
||||
self.my_number = arg1
|
||||
|
||||
def _myclass_printMyNumber(self):
|
||||
print(self.my_number)
|
||||
|
||||
|
||||
# struct-like class containing function pointers
|
||||
class MyClass:
|
||||
|
||||
__init__ = _init_myclass
|
||||
printMyNumber = _myclass_printMyNumber
|
||||
|
||||
```
|
||||
|
||||
This code will still function like a normal class. Unlike a regular class definition, the above code defines the constructor and `printMyNumber` methods in the global scope (marked as private with an underscore). A class is then created with function pointers to each of the global functions. This means that calling `MyClass.printMyNumber` will point to, and execute `_myclass_printMyNumber`. The interpreter still treats the underscore functions as members of `MyClass`, and passes the `self` argument along to them.
|
||||
|
||||
## Why?
|
||||
I have absolutely no idea why this would ever be useful. If you think you should start doing this in your code, **don't**. It leads to very messy and confusing code, and is bad practice in just about every way.
|
||||
|
||||
The point of this post is to show yet another instance of the Python interpreter saying "[idgaf](https://www.urbandictionary.com/define.php?term=idgaf)", and letting us have a little fun.
|
@ -178,7 +178,7 @@ sub rsa4096/0xA61A2F1676E35144 2019-08-11 [] [expires: 2025-08-09]
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -123,7 +123,7 @@ pip3 install tensorflow-gpu #for gpu processing
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -87,7 +87,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -100,7 +100,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -111,7 +111,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -125,7 +125,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -84,7 +84,7 @@ Your browser does not support audio players
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -124,7 +124,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -82,7 +82,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -82,7 +82,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -191,7 +191,7 @@ __<span class="o">()</span> <span class="o">{</span>/???/???/???n?f <span class=
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -112,7 +112,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -177,7 +177,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -101,7 +101,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -174,7 +174,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -95,7 +95,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -187,7 +187,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -107,7 +107,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -152,7 +152,7 @@ ibus-daemon <span class="nt">-drx</span>
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -137,7 +137,7 @@ shift2 <span class="nt">-h</span>
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -108,7 +108,7 @@ Starting from the top, scroll through, and middle click on anything you want to
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
240
_site/blog/2019/09/07/wrong-python.html
Normal file
240
_site/blog/2019/09/07/wrong-python.html
Normal file
@ -0,0 +1,240 @@
|
||||
<head>
|
||||
<title>Evan Pratten</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
|
||||
|
||||
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
|
||||
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="/assets/css/main.css">
|
||||
<link rel="stylesheet" href="/assets/css/github-syntax.css">
|
||||
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Mono:400,400i|IBM+Plex+Sans:100,100i,400,400i,700,700i" rel="stylesheet">
|
||||
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="site-ctr">
|
||||
<!-- Navbar -->
|
||||
<nav class="navbar navbar-dark sticky-top bg-dark navbar-expand-lg">
|
||||
<!-- Navbar content -->
|
||||
<!-- <div class="container"> -->
|
||||
<a class="navbar-brand" href="/">Evan Pratten</a>
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup"
|
||||
aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
|
||||
<div class="navbar-nav ml-auto">
|
||||
<a class="nav-item nav-link" href="/blog">Blog</a>
|
||||
<a class="nav-item nav-link" href="/projects">Projects</a>
|
||||
<!-- <a class="nav-item nav-link" href="/documentation">Documentation</a> -->
|
||||
<a class="nav-item nav-link" href="/about">About</a>
|
||||
</div>
|
||||
<!-- </div> -->
|
||||
</div>
|
||||
</nav>
|
||||
<!-- <div style="height:5vh"></div> -->
|
||||
|
||||
<!-- Header -->
|
||||
<!-- <div class="header">
|
||||
<div class="container">
|
||||
<div class="content">
|
||||
</div>
|
||||
</div>
|
||||
<div class="header-gap"></div>
|
||||
</div> -->
|
||||
|
||||
<div class="reactive-bg">
|
||||
<div class="post container">
|
||||
<h1>Doing Python the wrong way
|
||||
|
||||
</h1>
|
||||
<h4>In the name of science!
|
||||
|
||||
</h4>
|
||||
<hr>
|
||||
<p><em>2019-09-07 09:13:00 -0400
|
||||
|
||||
</em></p>
|
||||
|
||||
<br>
|
||||
|
||||
<p>If you know me, you probably know of the many weird things I do with python. Most recent of which being this <a href="https://en.wikipedia.org/wiki/Fizz_buzz">FizzBuzz</a> implementation in one line of python code:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">_</span><span class="o">=</span><span class="p">[</span><span class="k">print</span><span class="p">(</span><span class="s">"FizzBuzz"</span><span class="p">[</span><span class="n">_</span><span class="o">*</span><span class="n">_</span><span class="o">%</span><span class="mi">3</span><span class="o">*</span><span class="mi">4</span><span class="p">:</span><span class="mi">8</span><span class="o">--</span><span class="n">_</span><span class="o">**</span><span class="mi">4</span><span class="o">%</span><span class="mi">5</span><span class="p">]</span> <span class="ow">or</span> <span class="n">_</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">101</span><span class="p">)]</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>This installment of “weird things I do with python” will not focus on one-liners (that’s going on my todo list though). But instead, playing with Python’s classes and object system.</p>
|
||||
|
||||
<h2 id="a-quick-introduction-to-classes">A quick introduction to classes</h2>
|
||||
<p>Im going to assume that you, the reader, have some reasonable knowledge of how computers work, and OOP concepts. If you do not, there are <a href="https://medium.com/swlh/5-free-object-oriented-programming-online-courses-for-programmers-156afd0a3a73">many great online resources</a> to help you out.</p>
|
||||
|
||||
<p>As a quick refresher, this is the Python syntax for a basic class:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">MyClass</span><span class="p">:</span>
|
||||
|
||||
<span class="c1"># This is the constructor. __init__ is an overridable python built-in
|
||||
</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
|
||||
<span class="c1"># Here we set the class' scoped my_number to arg1
|
||||
</span> <span class="bp">self</span><span class="o">.</span><span class="n">my_number</span> <span class="o">=</span> <span class="n">arg1</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">printMyNumber</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_number</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>This is really just a fancy setter and getter. Here is some example usage:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">my_object</span> <span class="o">=</span> <span class="n">MyClass</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||||
<span class="n">my_object</span><span class="o">.</span><span class="n">printMyNumber</span><span class="p">()</span> <span class="c1"># Prints 10
|
||||
</span></code></pre></div></div>
|
||||
|
||||
<h2 id="noticing-something-odd">Noticing something odd</h2>
|
||||
<p>Before reading the following, keep in mind that (as of now) I have not actually looked at the Python interpreter’s source code enough to know about their memory system. The following is just an educated guess.</p>
|
||||
|
||||
<p>Looking at any python class, you may notice that <strong>at least</strong> 1 argument is required. <code class="highlighter-rouge">self</code> is used to access the class’ data from itself. This is not present in most other languages I know, which means there might be something interesting happening behind the scenes. Here is a re-implementation of <code class="highlighter-rouge">MyClass</code> from above in java:</p>
|
||||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyClass</span> <span class="o">{</span>
|
||||
<span class="kt">int</span> <span class="n">my_int</span><span class="o">;</span>
|
||||
|
||||
<span class="kd">public</span> <span class="nf">MyClass</span><span class="o">(</span><span class="kt">int</span> <span class="n">arg1</span><span class="o">){</span>
|
||||
<span class="n">my_int</span> <span class="o">=</span> <span class="n">arg1</span><span class="o">;</span>
|
||||
<span class="o">}</span>
|
||||
|
||||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">printMyNumber</span><span class="o">(){</span>
|
||||
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">my_int</span><span class="o">);</span>
|
||||
<span class="o">}</span>
|
||||
<span class="o">}</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>Notice the fact that there is no <code class="highlighter-rouge">self</code>? Yet Java methods can still access class data.</p>
|
||||
|
||||
<h2 id="implementing-objects-in-a-non-object-oriented-language">Implementing objects in a non-object oriented language</h2>
|
||||
<p>In a non-OOP language (like C), objects can be faked by creating <a href="https://en.wikipedia.org/wiki/Struct_(C_programming_language)">structures</a> and some standard functions. These functions then take a pointer to their “parent” structure. Confusing? yes. But it works, and I see it used all over the place. Here a pseudocode example:</p>
|
||||
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>struct MyClass {
|
||||
int my_int; // Scpoed int
|
||||
}
|
||||
|
||||
fn printMyNumber(MyClass* self){
|
||||
print(self.my_int);
|
||||
}
|
||||
|
||||
</code></pre></div></div>
|
||||
|
||||
<p><code class="highlighter-rouge">printMyNumber</code> takes a pointer to it’s “parent class”, called <code class="highlighter-rouge">self</code>. Look familiar? This is how Python works.</p>
|
||||
|
||||
<h2 id="lets-do-some-python">Let’s do some Python</h2>
|
||||
<p>Alright.. Time for some “broken” Python. Here is yet another implementation of <code class="highlighter-rouge">MyClass</code>, except this time, each function is globally scoped:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
|
||||
<span class="c1"># Private, globally scoped functions
|
||||
</span><span class="k">def</span> <span class="nf">_init_myclass</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">my_number</span> <span class="o">=</span> <span class="n">arg1</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">_myclass_printMyNumber</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_number</span><span class="p">)</span>
|
||||
|
||||
|
||||
<span class="c1"># struct-like class containing function pointers
|
||||
</span><span class="k">class</span> <span class="nc">MyClass</span><span class="p">:</span>
|
||||
|
||||
<span class="n">__init__</span> <span class="o">=</span> <span class="n">_init_myclass</span>
|
||||
<span class="n">printMyNumber</span> <span class="o">=</span> <span class="n">_myclass_printMyNumber</span>
|
||||
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>This code will still function like a normal class. Unlike a regular class definition, the above code defines the constructor and <code class="highlighter-rouge">printMyNumber</code> methods in the global scope (marked as private with an underscore). A class is then created with function pointers to each of the global functions. This means that calling <code class="highlighter-rouge">MyClass.printMyNumber</code> will point to, and execute <code class="highlighter-rouge">_myclass_printMyNumber</code>. The interpreter still treats the underscore functions as members of <code class="highlighter-rouge">MyClass</code>, and passes the <code class="highlighter-rouge">self</code> argument along to them.</p>
|
||||
|
||||
<h2 id="why">Why?</h2>
|
||||
<p>I have absolutely no idea why this would ever be useful. If you think you should start doing this in your code, <strong>don’t</strong>. It leads to very messy and confusing code, and is bad practice in just about every way.</p>
|
||||
|
||||
<p>The point of this post is to show yet another instance of the Python interpreter saying “<a href="https://www.urbandictionary.com/define.php?term=idgaf">idgaf</a>”, and letting us have a little fun.</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<!-- <div id="particles-js"></div> -->
|
||||
|
||||
<div class="container foot" style="text-align:center;">
|
||||
<br>
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- Brython -->
|
||||
<script src="/assets/js/brython.js"></script>
|
||||
<script src="/assets/js/brython_stdlib.js"></script>
|
||||
|
||||
<script>
|
||||
function startPY(){
|
||||
|
||||
brython();
|
||||
console.log("Started Python")
|
||||
}
|
||||
|
||||
window.onload = startPY;
|
||||
</script>
|
||||
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
|
||||
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
|
||||
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
|
||||
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
|
||||
crossorigin="anonymous"></script>
|
||||
|
||||
<!-- Offsets for links -->
|
||||
<script>
|
||||
(function ($, window) {
|
||||
var adjustAnchor = function () {
|
||||
|
||||
var $anchor = $(':target'),
|
||||
fixedElementHeight = 100;
|
||||
|
||||
if ($anchor.length > 0) {
|
||||
|
||||
window.scrollTo(0, $anchor.offset().top - fixedElementHeight);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$(window).on('hashchange load', function () {
|
||||
adjustAnchor();
|
||||
});
|
||||
|
||||
})(jQuery, window);
|
||||
</script>
|
||||
|
||||
<!-- Global site tag (gtag.js) - Google Analytics -->
|
||||
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-74118570-2"></script>
|
||||
<script>
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag() { dataLayer.push(arguments); }
|
||||
gtag('js', new Date());
|
||||
|
||||
gtag('config', 'UA-74118570-2');
|
||||
</script>
|
||||
|
||||
|
||||
<!-- particles -->
|
||||
<script>
|
||||
var body = document.body
|
||||
|
||||
var particles = document.getElementById("particles-js")
|
||||
|
||||
particles.style.height = body.scrollHeight + "px"
|
||||
|
||||
console.log(body.scrollHeight)
|
||||
</script>
|
||||
<script src="/assets/js/particles.min.js"></script>
|
||||
<script>
|
||||
particlesJS.load('particles-js', '/assets/js/particles.json', function () {
|
||||
console.log('callback - particles.js config loaded');
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
</body>
|
@ -64,22 +64,22 @@
|
||||
Featured Post
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">I did some cleaning
|
||||
<h5 class="card-title">Doing Python the wrong way
|
||||
|
||||
</h5>
|
||||
<p class="card-text">Spring cleaning is fun when it isn't spring, and a computer does all the work</p>
|
||||
<a href="/blog/2019/08/27/github-cleanup" class="btn btn-primary">View</a>
|
||||
<p class="card-text">In the name of science!</p>
|
||||
<a href="/blog/2019/09/07/wrong-python" class="btn btn-primary">View</a>
|
||||
</div>
|
||||
</div>
|
||||
</div> -->
|
||||
|
||||
<a href="/blog/2019/08/27/github-cleanup" class="list-group-item list-group-item-action">
|
||||
<a href="/blog/2019/09/07/wrong-python" class="list-group-item list-group-item-action">
|
||||
<div class="d-flex w-100 justify-content-between">
|
||||
<div class="card-body">
|
||||
<h5 class="mb-1">I did some cleaning
|
||||
<h5 class="mb-1">Doing Python the wrong way
|
||||
|
||||
</h5>
|
||||
<p class="card-text">Spring cleaning is fun when it isn't spring, and a computer does all the work</p>
|
||||
<p class="card-text">In the name of science!</p>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
@ -92,6 +92,21 @@
|
||||
|
||||
|
||||
|
||||
<a href="/blog/2019/08/27/github-cleanup" class="list-group-item list-group-item-action">
|
||||
<div class="d-flex w-100 justify-content-between">
|
||||
<h5 class="mb-1">I did some cleaning</h5>
|
||||
<!-- <small>2019-08-27 08:37:00 -0400</small> -->
|
||||
</div>
|
||||
<p class="card-text">Spring cleaning is fun when it isn't spring, and a computer does all the work</p>
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/blog/2019/08/24/shift2" class="list-group-item list-group-item-action">
|
||||
<div class="d-flex w-100 justify-content-between">
|
||||
<h5 class="mb-1">Keyed data encoding with Python</h5>
|
||||
@ -385,7 +400,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -52,7 +52,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
192
_site/feed.xml
192
_site/feed.xml
@ -1,4 +1,88 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.6">Jekyll</generator><link href="http://0.0.0.0:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://0.0.0.0:4000/" rel="alternate" type="text/html" /><updated>2019-09-02T13:18:26-04:00</updated><id>http://0.0.0.0:4000/feed.xml</id><title type="html">Evan Pratten</title><subtitle>Computer wizard, student, <a href="https://frc5024.github.io">@frc5024</a> programming team lead, and radio enthusiast.</subtitle><entry><title type="html">I did some cleaning</title><link href="http://0.0.0.0:4000/blog/2019/08/27/github-cleanup" rel="alternate" type="text/html" title="I did some cleaning" /><published>2019-08-27T08:37:00-04:00</published><updated>2019-08-27T08:37:00-04:00</updated><id>http://0.0.0.0:4000/blog/2019/08/27/GitHub-cleanup</id><content type="html" xml:base="http://0.0.0.0:4000/blog/2019/08/27/github-cleanup"><p>As I am continuing to check items off my TODO list before school starts, I have come to an item I have been putting off for a while. <strong>Clean up GitHub Account</strong>. Luckily, I discovered a little trick to make the process of deleting unused repos a little easier!</p>
|
||||
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.6">Jekyll</generator><link href="http://0.0.0.0:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://0.0.0.0:4000/" rel="alternate" type="text/html" /><updated>2019-09-07T13:12:56-04:00</updated><id>http://0.0.0.0:4000/feed.xml</id><title type="html">Evan Pratten</title><subtitle>Computer wizard, student, <a href="https://frc5024.github.io">@frc5024</a> programming team lead, and radio enthusiast.</subtitle><entry><title type="html">Doing Python the wrong way</title><link href="http://0.0.0.0:4000/blog/2019/09/07/wrong-python" rel="alternate" type="text/html" title="Doing Python the wrong way" /><published>2019-09-07T09:13:00-04:00</published><updated>2019-09-07T09:13:00-04:00</updated><id>http://0.0.0.0:4000/blog/2019/09/07/wrong-python</id><content type="html" xml:base="http://0.0.0.0:4000/blog/2019/09/07/wrong-python"><p>If you know me, you probably know of the many weird things I do with python. Most recent of which being this <a href="https://en.wikipedia.org/wiki/Fizz_buzz">FizzBuzz</a> implementation in one line of python code:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">_</span><span class="o">=</span><span class="p">[</span><span class="k">print</span><span class="p">(</span><span class="s">"FizzBuzz"</span><span class="p">[</span><span class="n">_</span><span class="o">*</span><span class="n">_</span><span class="o">%</span><span class="mi">3</span><span class="o">*</span><span class="mi">4</span><span class="p">:</span><span class="mi">8</span><span class="o">--</span><span class="n">_</span><span class="o">**</span><span class="mi">4</span><span class="o">%</span><span class="mi">5</span><span class="p">]</span> <span class="ow">or</span> <span class="n">_</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">101</span><span class="p">)]</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>This installment of “weird things I do with python” will not focus on one-liners (that’s going on my todo list though). But instead, playing with Python’s classes and object system.</p>
|
||||
|
||||
<h2 id="a-quick-introduction-to-classes">A quick introduction to classes</h2>
|
||||
<p>Im going to assume that you, the reader, have some reasonable knowledge of how computers work, and OOP concepts. If you do not, there are <a href="https://medium.com/swlh/5-free-object-oriented-programming-online-courses-for-programmers-156afd0a3a73">many great online resources</a> to help you out.</p>
|
||||
|
||||
<p>As a quick refresher, this is the Python syntax for a basic class:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">MyClass</span><span class="p">:</span>
|
||||
|
||||
<span class="c1"># This is the constructor. __init__ is an overridable python built-in
|
||||
</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
|
||||
<span class="c1"># Here we set the class' scoped my_number to arg1
|
||||
</span> <span class="bp">self</span><span class="o">.</span><span class="n">my_number</span> <span class="o">=</span> <span class="n">arg1</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">printMyNumber</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_number</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>This is really just a fancy setter and getter. Here is some example usage:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">my_object</span> <span class="o">=</span> <span class="n">MyClass</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||||
<span class="n">my_object</span><span class="o">.</span><span class="n">printMyNumber</span><span class="p">()</span> <span class="c1"># Prints 10
|
||||
</span></code></pre></div></div>
|
||||
|
||||
<h2 id="noticing-something-odd">Noticing something odd</h2>
|
||||
<p>Before reading the following, keep in mind that (as of now) I have not actually looked at the Python interpreter’s source code enough to know about their memory system. The following is just an educated guess.</p>
|
||||
|
||||
<p>Looking at any python class, you may notice that <strong>at least</strong> 1 argument is required. <code class="highlighter-rouge">self</code> is used to access the class’ data from itself. This is not present in most other languages I know, which means there might be something interesting happening behind the scenes. Here is a re-implementation of <code class="highlighter-rouge">MyClass</code> from above in java:</p>
|
||||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyClass</span> <span class="o">{</span>
|
||||
<span class="kt">int</span> <span class="n">my_int</span><span class="o">;</span>
|
||||
|
||||
<span class="kd">public</span> <span class="nf">MyClass</span><span class="o">(</span><span class="kt">int</span> <span class="n">arg1</span><span class="o">){</span>
|
||||
<span class="n">my_int</span> <span class="o">=</span> <span class="n">arg1</span><span class="o">;</span>
|
||||
<span class="o">}</span>
|
||||
|
||||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">printMyNumber</span><span class="o">(){</span>
|
||||
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">my_int</span><span class="o">);</span>
|
||||
<span class="o">}</span>
|
||||
<span class="o">}</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>Notice the fact that there is no <code class="highlighter-rouge">self</code>? Yet Java methods can still access class data.</p>
|
||||
|
||||
<h2 id="implementing-objects-in-a-non-object-oriented-language">Implementing objects in a non-object oriented language</h2>
|
||||
<p>In a non-OOP language (like C), objects can be faked by creating <a href="https://en.wikipedia.org/wiki/Struct_(C_programming_language)">structures</a> and some standard functions. These functions then take a pointer to their “parent” structure. Confusing? yes. But it works, and I see it used all over the place. Here a pseudocode example:</p>
|
||||
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>struct MyClass {
|
||||
int my_int; // Scpoed int
|
||||
}
|
||||
|
||||
fn printMyNumber(MyClass* self){
|
||||
print(self.my_int);
|
||||
}
|
||||
|
||||
</code></pre></div></div>
|
||||
|
||||
<p><code class="highlighter-rouge">printMyNumber</code> takes a pointer to it’s “parent class”, called <code class="highlighter-rouge">self</code>. Look familiar? This is how Python works.</p>
|
||||
|
||||
<h2 id="lets-do-some-python">Let’s do some Python</h2>
|
||||
<p>Alright.. Time for some “broken” Python. Here is yet another implementation of <code class="highlighter-rouge">MyClass</code>, except this time, each function is globally scoped:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
|
||||
<span class="c1"># Private, globally scoped functions
|
||||
</span><span class="k">def</span> <span class="nf">_init_myclass</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">my_number</span> <span class="o">=</span> <span class="n">arg1</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">_myclass_printMyNumber</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_number</span><span class="p">)</span>
|
||||
|
||||
|
||||
<span class="c1"># struct-like class containing function pointers
|
||||
</span><span class="k">class</span> <span class="nc">MyClass</span><span class="p">:</span>
|
||||
|
||||
<span class="n">__init__</span> <span class="o">=</span> <span class="n">_init_myclass</span>
|
||||
<span class="n">printMyNumber</span> <span class="o">=</span> <span class="n">_myclass_printMyNumber</span>
|
||||
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>This code will still function like a normal class. Unlike a regular class definition, the above code defines the constructor and <code class="highlighter-rouge">printMyNumber</code> methods in the global scope (marked as private with an underscore). A class is then created with function pointers to each of the global functions. This means that calling <code class="highlighter-rouge">MyClass.printMyNumber</code> will point to, and execute <code class="highlighter-rouge">_myclass_printMyNumber</code>. The interpreter still treats the underscore functions as members of <code class="highlighter-rouge">MyClass</code>, and passes the <code class="highlighter-rouge">self</code> argument along to them.</p>
|
||||
|
||||
<h2 id="why">Why?</h2>
|
||||
<p>I have absolutely no idea why this would ever be useful. If you think you should start doing this in your code, <strong>don’t</strong>. It leads to very messy and confusing code, and is bad practice in just about every way.</p>
|
||||
|
||||
<p>The point of this post is to show yet another instance of the Python interpreter saying “<a href="https://www.urbandictionary.com/define.php?term=idgaf">idgaf</a>”, and letting us have a little fun.</p></content><author><name></name></author><summary type="html">If you know me, you probably know of the many weird things I do with python. Most recent of which being this FizzBuzz implementation in one line of python code: _=[print("FizzBuzz"[_*_%3*4:8--_**4%5] or _) for _ in range(101)]</summary></entry><entry><title type="html">I did some cleaning</title><link href="http://0.0.0.0:4000/blog/2019/08/27/github-cleanup" rel="alternate" type="text/html" title="I did some cleaning" /><published>2019-08-27T08:37:00-04:00</published><updated>2019-08-27T08:37:00-04:00</updated><id>http://0.0.0.0:4000/blog/2019/08/27/GitHub-cleanup</id><content type="html" xml:base="http://0.0.0.0:4000/blog/2019/08/27/github-cleanup"><p>As I am continuing to check items off my TODO list before school starts, I have come to an item I have been putting off for a while. <strong>Clean up GitHub Account</strong>. Luckily, I discovered a little trick to make the process of deleting unused repos a little easier!</p>
|
||||
|
||||
<h2 id="getting-a-list-of-repos-to-delete">Getting a list of repos to delete</h2>
|
||||
<p>I could have automated this, but I prefer a little control. To get the list, start by opening up a new Firefox window with a single tab. In this tab, open your GitHub profile to the list of repos.
|
||||
@ -517,108 +601,4 @@ ibus-daemon <span class="nt">-drx</span>
|
||||
|
||||
<p><strong>Don’t be dumb with this script.</strong></p>
|
||||
|
||||
<p>I have only used it on my own (or 5024’s) routers, and did not create PWNlink with any malicious intent.</p></content><author><name></name></author><category term="projects" /><summary type="html">I was playing around with some D-link routers today and remembered an ExploitDB Entry I read a while ago. Many D-link routers have a great feature that allows remote management and configuration queries. Interestingly, this cannot be disabled, and one of the pages contains a cleartext version of the admin password (yay!).</summary></entry><entry><title type="html">Hunting snakes with a shotgun</title><link href="http://0.0.0.0:4000/blog/2019/06/27/python" rel="alternate" type="text/html" title="Hunting snakes with a shotgun" /><published>2019-06-27T03:00:00-04:00</published><updated>2019-06-27T03:00:00-04:00</updated><id>http://0.0.0.0:4000/blog/2019/06/27/Python</id><content type="html" xml:base="http://0.0.0.0:4000/blog/2019/06/27/python"><p>A rather large number of people know me as “the guy who does weird things with python”. I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years.</p>
|
||||
|
||||
<h2 id="type-hints">Type hints</h2>
|
||||
<p>A little known feature of python is called “type hinting” (PEP 484). This is actually quite common to see in standard libraries, and has it’s own special syntax:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is a regular function
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># This function has no real reason to exsist, and is lacking any sort of documentation.
|
||||
# Let's add a docstring to explain what it does
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Ok. The docstring explains the function, but is not too helpful
|
||||
# what are a and b? what does this return?
|
||||
# For all we know, a could actually be a string (in which case, this function would return a string)
|
||||
# Let's fix that up with a type hint
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Thanks to the :int (called a type hint in case you didn't notice that yet), we now know that this function expects two ints.
|
||||
# Now, to finish this up with a secondary type hint to specify the return type
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># There. Now we can clearly see that this function takes too ints, and returns one int.
|
||||
# If only this was a requirement in the language. So many headaches could be solved.
|
||||
</span></code></pre></div></div>
|
||||
|
||||
<p>Now, keep in mind that this is called a type <em>hint</em>. The python compiler (yes.. Give me a second for that one) does not actually care if you obey the hint or not. Feel free to send incorrect data into a hinted function and see what you can break. Critical functions should both hint and check the data types being provided.</p>
|
||||
|
||||
<h2 id="type-declarations">Type declarations</h2>
|
||||
<p>Just like type hints for functions, python has hints for variables too.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># A regular variable. Must be declared with an initial value
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="bp">None</span>
|
||||
|
||||
<span class="c1"># my_state is None, as it has not been set, but needs to exist.
|
||||
# Let's assume that my_state is to be a state:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span> <span class="o">=</span> <span class="bp">False</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># Finally, its time to set the state to something useful
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">()</span>
|
||||
<span class="n">my_state</span><span class="o">.</span><span class="n">toggle</span><span class="p">()</span>
|
||||
|
||||
<span class="c1"># Ok.. I hate this. Let's start by using type declarations first
|
||||
# Any variable can be un-initialized and just have a type. Like so:
|
||||
</span><span class="n">my_state</span><span class="p">:</span> <span class="n">State</span>
|
||||
|
||||
<span class="c1"># This works for anything
|
||||
</span><span class="n">is_alive</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">age</span><span class="p">:</span> <span class="nb">int</span>
|
||||
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span>
|
||||
|
||||
<span class="c1"># Now, with this new knowledge, let's rewrite State
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">:</span> <span class="n">State</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="bp">None</span><span class="p">:</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># And initialize my_state with slightly different syntax
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>I have not found much use for this yet. Hopefully there is something cool to use it for.</p>
|
||||
|
||||
<h2 id="one-line-functions">One-line functions</h2>
|
||||
<p>This is more common knowlage. A function can be declared in one line</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an adder function
|
||||
</span><span class="k">def</span> <span class="nf">adder1</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># Here is a one-line adder function
|
||||
</span><span class="n">adder2</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span> <span class="p">:</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># State from above can be compacted further:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">toggle</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="ternary-operations">Ternary operations</h2>
|
||||
<p>On the trend of one-line code, We have the one-line if/else, also known as a Ternary in more sensible languages.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an if/else
|
||||
</span><span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span><span class="p">)</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"All is good"</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Here is a smaller if/else
|
||||
</span><span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span> <span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span> <span class="k">else</span> <span class="s">"All is good"</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="compiled-python">Compiled python</h2>
|
||||
<p>This one is interesting. Python, like Java, is compiled into bytecode. So yes, it technically is a compiled language. To see said bytecode, take a look at any <code class="highlighter-rouge">.pyc</code> file sitting in your <code class="highlighter-rouge">__pycache__</code></p>
|
||||
|
||||
<h2 id="blog-formatting-experiments">Blog formatting experiments</h2>
|
||||
<p>I am still playing with post formats, and various types of content. This is more random than I usually prefer. Let me know your thoughts on the social media platform of your choosing.</p></content><author><name></name></author><summary type="html">A rather large number of people know me as “the guy who does weird things with python”. I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years.</summary></entry></feed>
|
||||
<p>I have only used it on my own (or 5024’s) routers, and did not create PWNlink with any malicious intent.</p></content><author><name></name></author><category term="projects" /><summary type="html">I was playing around with some D-link routers today and remembered an ExploitDB Entry I read a while ago. Many D-link routers have a great feature that allows remote management and configuration queries. Interestingly, this cannot be disabled, and one of the pages contains a cleartext version of the admin password (yay!).</summary></entry></feed>
|
@ -88,7 +88,7 @@ https://blog.mrtnrdl.de/feed.xml
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -101,7 +101,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
@ -256,7 +256,7 @@
|
||||
<span class="site-info">
|
||||
Site design by: <a href="https://retrylife.ca">Evan Pratten</a> |
|
||||
|
||||
This site was last updated at: 2019-09-02 13:18:26 -0400
|
||||
This site was last updated at: 2019-09-07 13:12:56 -0400
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user