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 @@ +
+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.
+ +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
+
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.
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.
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.
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.
+ +Spring cleaning is fun when it isn't spring, and a computer does all the work
- View +In the name of science!
+ ViewSpring cleaning is fun when it isn't spring, and a computer does all the work
+In the name of science!
Spring cleaning is fun when it isn't spring, and a computer does all the work
+ + + + + + + + +