Chapter 6 : classes Flashcards
What is a class?
Structure that contains data (variables) and logic (functions) in a reusable container
Sort of like a cookie cutter
What is an object?
The are created from classes so you can use those classes (data and logic)
What is a method?
When a function (logic) is added to a class
Create a class, object and method and define them
class world:
greeting = “Hello, World!”
def \_\_init\_\_(self): print(self.greeting) w = World()
1) Class is from the term class down to the print indent
2) The function is known as a method since it’s in a class
3) w = World is our object, since it allows us to use the class. Remember that the naming convention should normally be a capitalized word.
4) __init__ is a special method ( the initializer ) that is automatically called when an object is create from the method.
5) self is an argument used to reference the object created by the class. This just means that the variable it’s trying to access isn’t in the method but the class, so it needs to refernce itself to find its variable
Create a bug class and object, the object will be Andy.
The method will store all his bug attributes like the type of bug he is, color, size, and aggression. After you have that information, show just his size.
Andy should also be able to do some stuff that bugs do, like walk or run, give him these attributes and print him doing them!
class Bug:
def __init__(self, type, color, size, agression):
self.type = type
self.color = color
self.size = size
self.agression = agression
def walk(self): print("walking...") def run(self): print("running...") Andy = Bug("beetle", "black", "big", "agressive") print(Andy.size) Andy.walk()
https://www.youtube.com/watch?v=rJzjDszODTI
What’s a good way of thinking of objects/classes
Your class is what your thing is made up of, it’s good to think of it like a person, so a person is going to be made up of different things, like height, weight, and skin color.
The Object itself is going to refer to a specific person who has the foundation found in the class, but his attributes are what makes him who he is.
What’s a good way to read self as?
“This instance of the class
sort a list from least to greatest
list = [
“laugh”,
“cry”,
“fart”
]
list.sort()
Show the biggest number in a list
then show the smallest
then show the amount of elements in the list
max(list)
min(list)
len(list)
What are instance variables?
Variables used in an instance of a class
class man:
def __init__(self, name, age)
name and age are our instance variables
customer(“Nathan”, “23”)
Those are also instance variables
Class variables
Variable that are actually stored in the class
greeting = “Hello”
You can call these like this
object.greeting
If calling from inside the class you use
self.greeting
Use enter and exit, try to explain what they do
class customer:
def \_\_init\_\_(self, name, city): self.name = name self.city = city def \_\_enter\_\_(self): print("entering scope") return self def \_\_exit\_\_(self, exc_type, exc_value, traceback): print("Leaving scop") def greet(self): print("Hello, " + self.name + "!")
with customer(“Robert”, “Florence”) as robert:
robert.greet()
Basically what happens here is we create a temporary object “robert”
The with statement lets python know we’ll use the Enter and Exit methods
First __init__ runs and creates the instance variables
Next __enter__ runs whatever it needs to and then uses “return self” to denote that it’s finished.
Next, rober.greet() can run and do what it needs to
Finally the exit sequence gets a go and it runs some exception stuff to basically say “if something goes wrong, lets close this up so we don’t run the computers resources wild”, this is important if the program is doing something with a computer resource, like a file, if it malfunctioned without those excepts it would just leave it open and the world would end
Other Explanation
You use “with” to tell it to open and use the class, enter will start, then your code will be implemented in the scope of the class, finally when you’re finished your exit code and close out of whatever you got into.
For instance, maybe your enter opens a SQL database, then your code runs something in it, lastly the exit will close it up and make sure it doesn’t continue to run.
Delete your object and explain why using delete isn’t always ideal
class ferret:
def __init__(self):
print(“This is it”)
def __del__(self):
print(“Deleted”)
user = ferret()
del(user)
Sometimes this won’t even delete what you’ve asked it to
if the code is large and error prone it can affect python’s garbage collection which cleans up uneeded object so they don’t take so many resources.
Also, this could cause memory leaks which garbage cleanup tries to prevent
“__del__” Tells your program to delete the object when there are no more references to it, whereas del(object) just deletes it.
Say that you have created the code below, but in at some point you have to change the object’s first name (maybe the person had a name change) for the rest of your program. Your code is huge so you don’t want to retype every instance of referencing a method in the code, what would you do:
class Person:
def __init__(self, name, age, city):
self.name = name self.age = age self.city = city self.introduction = f"Hello, my name is {self.name}, and I'm {self.age}"
class Person:
def \_\_init\_\_(self, name, age, city): self.name = name self.age = age self.city = city @property def introduction(self): return f"Hello, my name is {self.name}, and I'm {self.age}"
me = Person(“Nathan”, 33, “Clarksville”)
me.name = “Bill”
print(me.introduction)
remember, this works because when you’re getting introduction, you do it like you did when it was just a variable before me.introduction
if it were a method it would look like this:
me.introduction()
Describe private variables, then show a coding example of them
Private variables are variables that we want to remain in a class and not accessed outside of it.
Private variables begin with and underscore like the below example. If it wasn’t private, and didn’t have the underscore, we would access it like this
class Demons:
def __init__(self, color):
self._color = color
@property
def color(self):
return self._color
If it wasn’t private, and didn’t have the underscore, we would access it like this:
me = Demons()
me.color
But, since it’s private we have to respect that, but here’s the work around…
Properties can be summoned just like variables, so if instead we use the property above, name the function color, then return self._color, we’re technically not returning the private variable, it’s just the property color, done the same way
me = Demons()
me.color