Skip to content

Instantly share code, notes, and snippets.

@MrMagicPenguin
Created December 13, 2023 02:18
Show Gist options
  • Save MrMagicPenguin/09aaab0d6fe7d8f75b8f730184ee2523 to your computer and use it in GitHub Desktop.
Save MrMagicPenguin/09aaab0d6fe7d8f75b8f730184ee2523 to your computer and use it in GitHub Desktop.
# this is a class
class Crop:
#This is how you define an instance of a class.
# __init__ is the 'constructor' or the function that is used when you generate a new instance of the class.
# notice that one of the arguments in the constructor is self.
# When you refer to 'self', you are referring to a property or function stored in the instanced class.
# 'self' is always passed as an argument when defining a function within a class - it needs to know its talking about itself!
def __init__(self, cropType):
# As part of creating an instance of the Crop class, we want to assign a cropType to it.
# we are taking the argument passed in the Constructor, and assigning it to a variable called cropType.
self.cropType = cropType
# Classes can have other functions defined by you.
# To be pedantic, functions of a class are called "Methods"
def harvest(self): # notice how we are passing Self as an argument to this method!
print("You harvested a " + self.cropType) # when this method is called, we print this to the console (text output thing)
# Here we are making a new Instance of the Crop class.
# Notice that we are not manually adding the 'self' keyword here! That is already handled in the Class definition.
# All we need is to tell it what type of crop it is.
# Also note that this Instance is being assigned to a variable!
# This means we can refer to it by an assumed name later on
tomato = Crop("tomato")
# Since we have decided to call this Crop instance "tomato", we can access methods or properties defined within it using the '.' operator.
# this is like saying "from" the tomato.
# From the tomato, perform the harvest action.
#tomato.harvest()
# We can expand on the above a little further:
class Garden:
# Again this is a constructor, but this time we are passing a 'Crop' when we make it!
def __init__(self, crop):
# This grid property contains all the crops we want in our garden.
# For now, lets plant it at position 0,1.
# We **aren't** putting self in front of crop inside the grid, because the Crop isn't a property of the Garden inherently!
# its just a piece of data we are putting at a certain position - the same way our letters are just pieces of data at a point.
# The Garden has no inherent relation to the Crop object - its just storing it for us.
self.grid = [
["A", "B"], # X or left/right coord
["C", "D"] # Y or up/down coord
]
# Lets start our garden by planting a banana.
# its a warm climate.
banana = Crop("Banana")
myGarden = Garden(banana)
# We know we planted the our crop at the coordinates 0,1 - so we can access it this way
# notice that here we are using .cropType to get the type of the Crop
# this might seem weird, because ultimately we are printing something from the Garden object, right? and it has no defined property of 'cropType'
# but like when we planted it, we are actually just storing a Crop object at a location in the garden.
# We're just retrieving that Crop's type after getting it.
#print(myGarden.grid[0][1].cropType)
# Try uncommenting these print statements and see what happens.
# print(myGarden.grid[0][1])
# print(type(myGarden.grid[0][1]))
# Another way you could write the above print statement is this:
myCrop = myGarden.grid[0][1]
#print(myCrop.cropType)
# We can do the same sort of things we did with the tomato, like harvest it!
myCrop.harvest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment