Katemonkey (In Most Places)

Learning Python, Day 13 — Classes and Objects

Even after I finish all this Python coursework, I'm so keeping my little weather converter around in Colab.

def tempconvert(c_temp):
  f_temp = (c_temp * 1.8) + 32
  print(f_temp, "or", c_temp)

tempconvert(10)

>> 50.0 or 10

Hope the laundry is enjoying the 50°F weather while it's on the line.

This is the last chunk of this particular course. I'm kinda sad to see it go, because it actually taught me plenty of things, but I'm also looking forward to whatever I decide to take next.

21 — Classes and Objects

"Objects are entities with state/properties and behaviour. State is a mix of all the values of an object. Behaviour typically modifies the state."

Why on earth does this sound like "I am 18 and high for the first time" rather than "I am studying programming"?

And what do you mean "try to keep things modeled after real life as possible"? Why am I learning programming then? I don't want real life, I want programming!

And is it "initialiser" or "initializer"?

I'm sure this'll all make a lot more sense once I actually see examples, but, man, there has to be a better way to explain all this.

Especially when it comes to inheritance. The instructor is definitely not explaining this well.

22 — Classes Examples

Huh. So you use Camel Case for classes. I checked the PEP too. I don't know if I'm going to remember this, but it's nice to see that they're following the standard.

I'm going to have to stick to slight variations of the example the instructor is giving me, because I'm still not 100% sure what I'm doing. I'm hoping, that as we go along, I'll be able to create my own program based on this.

I mean, it's still Wicker Man themed, but it's like if I was making a Wicker Man video game, since that's the example they're using.

class SummerisleOfficer:
	def __init__(self, name, x_pos, clues, suspicion):
		self.name = name
		self.x_pos = x_pos
		self.clues = clues
		self.suspicion = suspicion
	def move(self, by_amount):
		self.x_pos += by_amount
	def village(self, discovery):
		self.clues += discovery
		if self.clues >= 100:
			self.suspicion = 100
	def damage(self, amount):
		self.suspicion += amount
		if self.suspicion > 100:
			self.suspicion = 100
	def fire(self):
		return self.suspicion >= 100

So I have a class, the Summerisle Officer. That Officer has a self, a name, a particular position they are in on the X-axis, a collection of clues, and a growing suspicion.

When they move, they move by adding an amount to their X-axis position. When they are in the village, they gain clues by making discoveries. And if they're getting more suspicious, eventually they will be in a fire. This will also happen if they collect too many clues. You can only discover so much before it's time for our more dreadful sacrifice.

So I can see what each bit is going to do, I just need to figure out how I can apply it to other concepts.

23 — Objects Examples

It's time to make the first object.

officer = SummerisleOfficer("Howie", 0, 10, 50)
print(officer.name)

>> Howie

There we go, Officer Howie. He starts at 0, because he's just gliding his little seaplane into the harbour. He has 10% clues (the photo and the letter), and he's 50% suspicious, because maybe something's happened, maybe something hasn't.

officer = SummerisleOfficer("Howie", 0, 10, 50)
officer.move(10)
officer.village(25)
officer.damage(10)
print(officer.x_pos, officer.clues, officer.suspicion)

>> 10 35 60

Now he's moved to the pub. Good. He has more clues, and his suspicion is increasing, because they're weird people at the pub.

24 — Inheritance Examples

Since I have to make a subclass of SummerisleOfficer, I'm going to make the original more generic.

class Summerisle:
	def __init__(self, name, x_pos, clues):
		self.name = name
		self.x_pos = x_pos
		self.clues = clues
	def move(self, by_amount):
		self.x_pos += by_amount
	def village(self, discovery):
		self.clues += discovery

It starts the same as the SummerisleOfficer, but it doesn't have suspicion or fire, because while they have a bunch of clues and move around, they're not particularly suspicious of what's happening.

Then we bring in our Officer.

class SummerisleOfficer(Summerisle):
  def __init__(self, name, x_pos, clues, suspicion):
    super().__init__(name, x_pos, clues)
    self.suspicion = suspicion
  def village(self, discovery):
    self.clues += discovery
    if self.clues >= 100:
      self.suspicion = 100
  def damage(self, amount):
    self.suspicion += amount
    if self.suspicion > 100:
      self.suspicion = 100
  def fire(self):
    return self.suspicion >= 100

The officer has the same ability to have a name, move around, and have clues as the Summerisle villager, but they're also suspicious and have a chance to end up in the fire.

So then if I produce two characters from this...

officer = SummerisleOfficer("Howie", 0, 10, 50)
lord = Summerisle("Lord Summerisle", 50, 100)
print(officer.name, officer.x_pos, officer.clues, officer.suspicion)
print(lord.name, lord.x_pos, lord.clues)

>> Howie 0 10 50
>> Lord Summerisle 50 100

Officer Howie is in the harbour with a small amount of clues, and a good chunk of suspicion. Lord Summerisle is in his manor and knows everything.

Indenting is giving me the biggest grief at the moment. I think I have it down, and then Colab is like "Your idents don't match up". Oh my god, Colab, you need to make the indents more obvious then.

officer.village(90)
lord.village(0)
print(officer.clues, officer.suspicion)
print(lord.clues)

>> 100 100
>> 100

When Officer Howie gets all the clues from the village, his suspicion skyrockets to 100%. Lord Summerisle, of course, already has all the clues.

25 — Static Members Examples

I like the idea of having a static variable or a static function in the superclass.

class Summerisle:
  pagan_belief = 100
  def __init__(self, name, x_pos, clues):
    self.name = name
    self.x_pos = x_pos
    self.clues = clues
  def move(self, by_amount):
    self.x_pos += by_amount
  def village(self, discovery):
    self.clues += discovery
  def convert(to_convert):
    Summerisle.pagan_belief = to_convert

In this case, each Summerisle resident has pagan beliefs, but they also have a conversion option.

lord = Summerisle("Lord Summerisle", 50, 100)
print(lord.pagan_belief, lord.clues)

>> 100 100

I'm still not sure about the convert(to_convert) bit, because it's not turning out the right results, but I'm also getting fried, so it could be anything causing that. Me putting it somewhere in Colab and forgetting. Me not getting this part. Whatever. It's fine, I'm moving on.

26 — Summary and Outro

The instructor just goes through their slides again, going through all the things that have been covered, which is...fine, I guess.

And then there's a "Where to go from here" bit:

What annoys me about this list is that it doesn't really explain where to go. Like, great, fantastic, concepts. Libraries. Whatever. Give me a task, dammit. Point me to a repository. Something.

But I suppose they can't, really. I mean, everyone's going to be learning this for different reasons, so you can't really come up with a generic next step.

On the other hand, I now have a certificate.

BEHOLD!

A certificate declaring that I have finished the Python Programming Fundamentals course.

Day 13 — Results

My next steps? I'm not sure yet. I mean, I could go right into Web Development Fundamentals with HTML, CSS, and Bootstrap — Complete Guide, which would be pretty damn easy up until the Bootstrap bit, which would probably annoy the hell out of me.

Or I can stick with Python and start with the 7-hour Python Programming Fundamentals for n8n, which could then lead into Automate Excel with OpenPyXL In Python, From Excel to Python: Transform Data Science Workflows, Automate Excel with Data Engineering and Machine Learning in Python, and Automate Data Mining with Regex in Python and Excel.

I think the Bootstrap is going to annoy me more than data science workflows, so, yeah, next time, it's Python Programming Fundamentals.

Today's Sticker

A sticker with a selection of iconic images from the original The Wicker Man film, including the Salmon of Knowledge, the Hand of Glory, the Green Man mask, a hare, a police officer's hat, and a sickle held aloft. There is also the titular Wicker Man.

Aamina Mahmood's amazing The Wicker Man print as a sticker. She's apparently stopped doing them as stickers, but you can still get the print version. (Whereas I purchased this Wicker Man print.)

(And I also just realised I also have her A Matter of Life and Death postcard. Dang. Such good art!)

#kate learns python #programming