A #Python student showed me this code, and asked how to improve it:
def f(g, **kwargs):
for k, v in kwargs.items():
if "x1" == k:
g.x1 = v
elif "x2" == k:
g.x2 = v
elif "x3" == k:
g.x3 = v
First, what's this function doing?
- It gets an object and a dict of keyword arguments
- It goes through each keyword arg
- For three keys, it creates an attribute of that name on the object (g), with the value.
Let's assume the function can work for *all* keys. What can we do?
Well, we can always assign an attribute to nearly any Python object:
a.b = 100
This adds an attribute "b" to the object "a", with a value of 100 (an int).
We can accomplish the same with the "setattr" builtin:
Many #Python beginners are confused by space characters, empty strings, and the term "whitespace." After all, how can nothing be something? (Or: How can something be nothing?)
A thread about nothing!
(Cue the Seinfeld theme, I guess...)
Given:
s = 'a b'
This string contains three characters. As humans, we only see the "a" and "b", two characters separated by a space.
But computers don't work that way: The space character is a character. It takes up just as much space in memory as either "a" or "b".
We can see this if we iterate over the characters in s, printing their Unicode numbers:
>>> s = 'a b'
>>> for c in s:
... print(ord(c))
...
97
32
98
The first line scrapes the Wikipedia page for the Apollo program, putting all HTML tables into data frames. The missions are in the third table, aka index 2.
The second line turns lines containing date ranges into single (launch) dates, also removing commas and hyphens.
That second line then takes the resulting cleaned-up date strings, and passes them to pd.to_datetime. The resulting datetime series is then assigned back to df['Date'].
For about a year now, I've been upset with the unvaccinated. Why don't they, or won't they, get vaccinated? Are they suicidal, ignorant, or sociopathic?
Two great books have changed my thinking: High Conflict, by @amandaripley, and Empire of Pain, by @praddenkeefe.
A thread.
First, just to make it clear: I'm vaccinated (3 shots). I think the covid vaccines are among the greatest achievements of modern science. My family all got vaccinated ASAP. They work, and save lives. Everyone should get vaccinated.
So my struggle hasn't been about the vaccines. Rather, it's how so many people have refused something so obviously beneficial, which will save not only their own lives, but the lives of people they love.
The evidence is overwhelming. So why the heck aren't they getting shots?
Soon after you start to learn #Python, you start to hear that some data is mutable (i.e., can be changed), whereas other data is immutable (i.e., cannot be changed).
I find that many developers confuse "immutable" with "constant." These are very different ideas.
To appreciate the difference, remember that a Python variable is a reference to an object. It is *not* an alias for a location in memory.
So when you say "x = 5", you aren't sticking 5 in x's memory location. Rather, you are saying that the name "x" is another way to refer to 5.
In that sense, variables in Python are sort of like pronouns. You can refer to the object itself, or you can refer to it via its pronoun. However you refer to it, you get the same object.
When you assign a variable, you're saying that it (the pronoun) now refers to a new object.