diff --git a/_posts/2019-09-04-wrong-python.md b/_posts/2019-09-04-wrong-python.md new file mode 100644 index 0000000..d5ea604 --- /dev/null +++ b/_posts/2019-09-04-wrong-python.md @@ -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. \ No newline at end of file diff --git a/_site/about/index.html b/_site/about/index.html index 7199ce2..db53d4f 100644 --- a/_site/about/index.html +++ b/_site/about/index.html @@ -178,7 +178,7 @@ sub rsa4096/0xA61A2F1676E35144 2019-08-11 [] [expires: 2025-08-09] Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2018/06/27/becomeranter.html b/_site/blog/2018/06/27/becomeranter.html index 1f960c6..2081060 100644 --- a/_site/blog/2018/06/27/becomeranter.html +++ b/_site/blog/2018/06/27/becomeranter.html @@ -123,7 +123,7 @@ pip3 install tensorflow-gpu #for gpu processing Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/04/30/frc-languages.html b/_site/blog/2019/04/30/frc-languages.html index cb0da95..39f1529 100644 --- a/_site/blog/2019/04/30/frc-languages.html +++ b/_site/blog/2019/04/30/frc-languages.html @@ -87,7 +87,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/05/27/building-safe-vision-comms.html b/_site/blog/2019/05/27/building-safe-vision-comms.html index 24ec62c..fb1700f 100644 --- a/_site/blog/2019/05/27/building-safe-vision-comms.html +++ b/_site/blog/2019/05/27/building-safe-vision-comms.html @@ -100,7 +100,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/12/styiling-github.html b/_site/blog/2019/06/12/styiling-github.html index 9c03785..a275e45 100644 --- a/_site/blog/2019/06/12/styiling-github.html +++ b/_site/blog/2019/06/12/styiling-github.html @@ -111,7 +111,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/16/graphing-w2a.html b/_site/blog/2019/06/16/graphing-w2a.html index 6a5fc97..fc69e07 100644 --- a/_site/blog/2019/06/16/graphing-w2a.html +++ b/_site/blog/2019/06/16/graphing-w2a.html @@ -125,7 +125,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/17/amm2m1-release.html b/_site/blog/2019/06/17/amm2m1-release.html index 4b6297c..46f333a 100644 --- a/_site/blog/2019/06/17/amm2m1-release.html +++ b/_site/blog/2019/06/17/amm2m1-release.html @@ -84,7 +84,7 @@ Your browser does not support audio players Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/21/robot-experiences.html b/_site/blog/2019/06/21/robot-experiences.html index 2495103..b91963c 100644 --- a/_site/blog/2019/06/21/robot-experiences.html +++ b/_site/blog/2019/06/21/robot-experiences.html @@ -124,7 +124,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/23/googlectf.html b/_site/blog/2019/06/23/googlectf.html index 766498e..24a570b 100644 --- a/_site/blog/2019/06/23/googlectf.html +++ b/_site/blog/2019/06/23/googlectf.html @@ -82,7 +82,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/24/languagehunt2.html b/_site/blog/2019/06/24/languagehunt2.html index 73b6bb2..0dfb46c 100644 --- a/_site/blog/2019/06/24/languagehunt2.html +++ b/_site/blog/2019/06/24/languagehunt2.html @@ -82,7 +82,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/26/bashsmash.html b/_site/blog/2019/06/26/bashsmash.html index 1c94ee3..b5ee7f3 100644 --- a/_site/blog/2019/06/26/bashsmash.html +++ b/_site/blog/2019/06/26/bashsmash.html @@ -191,7 +191,7 @@ __() {/???/???/???n?f Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/27/pwnlink.html b/_site/blog/2019/06/27/pwnlink.html index 70ca483..654a44f 100644 --- a/_site/blog/2019/06/27/pwnlink.html +++ b/_site/blog/2019/06/27/pwnlink.html @@ -112,7 +112,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/06/27/python.html b/_site/blog/2019/06/27/python.html index e089e0d..18ad2e8 100644 --- a/_site/blog/2019/06/27/python.html +++ b/_site/blog/2019/06/27/python.html @@ -177,7 +177,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/07/01/devdns.html b/_site/blog/2019/07/01/devdns.html index 87d2bcd..925cfae 100644 --- a/_site/blog/2019/07/01/devdns.html +++ b/_site/blog/2019/07/01/devdns.html @@ -101,7 +101,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/07/06/scrapingfrcgithub.html b/_site/blog/2019/07/06/scrapingfrcgithub.html index 87b925d..ed8a114 100644 --- a/_site/blog/2019/07/06/scrapingfrcgithub.html +++ b/_site/blog/2019/07/06/scrapingfrcgithub.html @@ -174,7 +174,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/07/13/lookback-gmad.html b/_site/blog/2019/07/13/lookback-gmad.html index 9a39965..575b718 100644 --- a/_site/blog/2019/07/13/lookback-gmad.html +++ b/_site/blog/2019/07/13/lookback-gmad.html @@ -95,7 +95,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/07/15/mindmap.html b/_site/blog/2019/07/15/mindmap.html index 7bfe94e..7a3d0e8 100644 --- a/_site/blog/2019/07/15/mindmap.html +++ b/_site/blog/2019/07/15/mindmap.html @@ -187,7 +187,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/08/10/why-i-carry-nfc.html b/_site/blog/2019/08/10/why-i-carry-nfc.html index a6e1682..fa65d28 100644 --- a/_site/blog/2019/08/10/why-i-carry-nfc.html +++ b/_site/blog/2019/08/10/why-i-carry-nfc.html @@ -107,7 +107,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/08/12/setting-up-ja.html b/_site/blog/2019/08/12/setting-up-ja.html index c4e49c0..8d887fb 100644 --- a/_site/blog/2019/08/12/setting-up-ja.html +++ b/_site/blog/2019/08/12/setting-up-ja.html @@ -152,7 +152,7 @@ ibus-daemon -drx Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/08/24/shift2.html b/_site/blog/2019/08/24/shift2.html index 40c2074..e72bf97 100644 --- a/_site/blog/2019/08/24/shift2.html +++ b/_site/blog/2019/08/24/shift2.html @@ -137,7 +137,7 @@ shift2 -h Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/08/27/github-cleanup.html b/_site/blog/2019/08/27/github-cleanup.html index 9dfb19d..911cb24 100644 --- a/_site/blog/2019/08/27/github-cleanup.html +++ b/_site/blog/2019/08/27/github-cleanup.html @@ -108,7 +108,7 @@ Starting from the top, scroll through, and middle click on anything you want to Site design by: Evan Pratten | - 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 diff --git a/_site/blog/2019/09/07/wrong-python.html b/_site/blog/2019/09/07/wrong-python.html new file mode 100644 index 0000000..a743aea --- /dev/null +++ b/_site/blog/2019/09/07/wrong-python.html @@ -0,0 +1,240 @@ + + Evan Pratten + + + + + + + + + + + + + +
+ + + + + + + +
+
+

Doing Python the wrong way + +

+

In the name of science! + +

+
+

2019-09-07 09:13:00 -0400 + +

+ +
+ +

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)]
+
+ +

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 to help you out.

+ +

As a quick refresher, this is the Python syntax for a basic class:

+
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:

+
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:

+
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 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:

+

+# 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”, and letting us have a little fun.

+ +
+
+ +
+ + +
+
+ + Site design by: Evan Pratten | + + This site was last updated at: 2019-09-07 13:12:56 -0400 + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/_site/blog/index.html b/_site/blog/index.html index 6550cbe..a0da7df 100644 --- a/_site/blog/index.html +++ b/_site/blog/index.html @@ -64,22 +64,22 @@ Featured Post
-
I did some cleaning +
Doing Python the wrong way
-

Spring cleaning is fun when it isn't spring, and a computer does all the work

- View +

In the name of science!

+ View
--> - +
-
I did some cleaning +
Doing Python the wrong way
-

Spring cleaning is fun when it isn't spring, and a computer does all the work

+

In the name of science!

@@ -92,6 +92,21 @@ + +
+
I did some cleaning
+ +
+

Spring cleaning is fun when it isn't spring, and a computer does all the work

+
+ + + + + + + +
Keyed data encoding with Python
@@ -385,7 +400,7 @@ Site design by:
Evan Pratten | - 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
diff --git a/_site/documentation.html b/_site/documentation.html index 9f9f48a..6657452 100644 --- a/_site/documentation.html +++ b/_site/documentation.html @@ -52,7 +52,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/feed.xml b/_site/feed.xml index 4eadd74..1a87ece 100644 --- a/_site/feed.xml +++ b/_site/feed.xml @@ -1,4 +1,88 @@ -Jekyll2019-09-02T13:18:26-04:00http://0.0.0.0:4000/feed.xmlEvan PrattenComputer wizard, student, <a href="https://frc5024.github.io">@frc5024</a> programming team lead, and radio enthusiast.I did some cleaning2019-08-27T08:37:00-04:002019-08-27T08:37:00-04:00http://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> +Jekyll2019-09-07T13:12:56-04:00http://0.0.0.0:4000/feed.xmlEvan PrattenComputer wizard, student, <a href="https://frc5024.github.io">@frc5024</a> programming team lead, and radio enthusiast.Doing Python the wrong way2019-09-07T09:13:00-04:002019-09-07T09:13:00-04:00http://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>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)]I did some cleaning2019-08-27T08:37:00-04:002019-08-27T08:37:00-04:00http://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>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!).Hunting snakes with a shotgun2019-06-27T03:00:00-04:002019-06-27T03:00:00-04:00http://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>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. \ No newline at end of file +<p>I have only used it on my own (or 5024’s) routers, and did not create PWNlink with any malicious intent.</p>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!). \ No newline at end of file diff --git a/_site/fossl-feeds.html b/_site/fossl-feeds.html index 6377f55..06c4dfc 100644 --- a/_site/fossl-feeds.html +++ b/_site/fossl-feeds.html @@ -88,7 +88,7 @@ https://blog.mrtnrdl.de/feed.xml Site design by: Evan Pratten | - 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 diff --git a/_site/index.html b/_site/index.html index 70d623e..0dc5683 100644 --- a/_site/index.html +++ b/_site/index.html @@ -101,7 +101,7 @@ Site design by: Evan Pratten | - 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 diff --git a/_site/projects.html b/_site/projects.html index cb5b047..6fda2c3 100644 --- a/_site/projects.html +++ b/_site/projects.html @@ -256,7 +256,7 @@ Site design by: Evan Pratten | - 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