Below you will find pages that utilize the taxonomy term “python”
November 10, 2022
2022 11 10 Python 3.11 Error Reporting
Went to a Python meetup today where the new features in Python 3.11 were discussed. Among them was of course the new nifty error reporting. It was noted that the error reporting does NOT work when using the REPL, but only when running a script through the interpreter.
It wasn’t immediately apparent why that was, so I wanted to do a little bit of digging and see if I could find anything about it.
November 1, 2022
Shebang issue - Ubuntu does not link 'python' to 'python3' by default
I typically use #!/usr/bin/env python as my shebang for Python stuff. I switched to Ubuntu (22.04) from Fedora (36) recently and found that when I tried to run something outside of a virtualenv it bombed out thusly:
/usr/bin/env: ‘python’: No such file or directory This stackoverflow post offered two solutions:
sudo ln -s /usr/bin/python3 /usr/bin/python
sudo apt install python-is-python3
The post that presented the latter pointed me at the package’s description:
May 19, 2021
Python Decorators
On the subject of Decorators. Right now my brain is feeling overwhelmed and I wonder if I bit off more than I could chew. Not sure exactly which is best to start with but I’ll first go for the how and follow up with some of the why.
Also this article assumes some knowledge of decorators already. It’s not intended to be a full course on how they work or the motivation behind them (though I attempt to shed light on both).
May 10, 2021
Interesting Python Split Behavior
Had a recent challenge that didn’t hold me up for long. I tried what was the correct solution on a whim and it happened to work, but I wanted to analyze the behavior. The goal was to build a function that accepts a string and modifies it according to the examples:
"This is an example!" ==> "sihT si na !elpmaxe" "double spaces" ==> "elbuod secaps" The characters in each individual word are reversed, the ordering of the words are preserved as are the number of spaces between words.
May 3, 2021
Variadic Arguments
I’m currently working on a challenge that needs to check for an arbitrary amount of arguments.
The Python docs have info on some so-called ‘variadic arguments’ which are what I seem to need to be using. The idea is to use these fancy *args and **kwargs arguments which then allow one to pass as many positional or keyword arguments as desired and just handle them within the function when/if they are present.
April 30, 2021
List Comprehension
Well here I go again re-learning some stuff. I love list comprehension. I just did a challenge on some website where the description was:
The main idea is to count all the occurring characters in a string. If you have a string like aba, then the result should be {‘a’: 2, ‘b’: 1}.
What if the string is empty? Then the result should be empty object literal, {}.
So I of course went through and did my for loop, and nested an if/else within and some other stuff that wasn’t terribly elegant.
April 29, 2021
Python for loop over a dictionary
Jeeze, just had to remind myself of this. I got the blog up and going again to be able to note stuff like this down so here we go.
For loops on a dictionary in Python. There’s an option of looping over it raw, or include the use of its methods: keys(), values(), items().
When looping over it raw, you just get the keys back (makes me wonder why keys() exists in the first place, but that’s a lesson for another day):
November 14, 2017
Sublime Text 3 builder for Python 3
I didn’t know this but there’s this thing in Sublime Text 3 referred to as a “builder” that basically lets you run some code against whatever file/project you’re in.
The reason I’m using this is because I hate having a separate terminal open to run a script I’m working on. Instead I can just use ctrl+b and it’ll run the code and display the output in a window at the bottom of my Sublime window.
June 1, 2016
Python local variables
Apparently variables are local to a given function simply by virtue of being defined within said function. No keyword needed:
def f(): s = 'some value' print(s) ERROR: You fool! s isn't defined!!