CCChapter 8 and maybe more Flashcards
Use a dataclass to automatically create your __init__ and your instance variables
Give one default argument
from dataclasses import dataclass
@dataclass
class Customer:
name: str
city: str
age: int = 33
What are things like @dataclass and @property called?
decorators
Add all arguments to the dataclass decorator and describe their functionality
@dataclass(init=True, repr=True, eq=True, order=False, frozen=False)
init - defaults to true and auto creates __init__ method
repr - defaults to True and allows class to represent itself as a string, used to compare object usually. repr() will call this method on the class to obtain the string.
eq - defaults to true and the __eq__ method will compare two object of the same class.
order - defaults to false and does comparisons like equals, except with __lt__ __le__ __gt__ __ge__
frozen - defaults to false, if true it makes everything in class immutable which would allow it to be stored in a set or dictionary
Use the repr argument
from dataclasses import dataclass
@dataclass
class Customer:
name = str
age = int
c1 = Customer(“Nathan”, 33)
print(repr(c1))
Use the eq argument
from dataclasses import dataclass
@dataclass
class Customer:
name = str
age = int
c1 = Customer(“Nathan”, 33)
c2 = Customer(“Jeremy”, 28)
print(c1 == c2)
Module
Group of code that can be classes, variables, or functions all working together, a module works in it’s own namespace separate from the Main Program (global namespace)
Namespaces and importing
Importing grabs a module and makes it accessible to you. The module exists in it’s own namespace, like the module “locale”.
If we import locale:
import locale we can’t access locale_alias because it exists in the module’s namespace. You access it like this
locale.locale_alias
Give the locale module a new name
import locale as robert
robert.locale_alias
Lets say we want to use the parser code from the email module and that’s it, how would we do that?
from email import parser
Show all variables, then show all methods, then finally show helpful info for the email module
vars(email)
dir(email)
help(email)
How would you create a module
Save a file that you have made for a class that can actually do something.
Open a new file.
import distance (name of the file minus the .py bit)
dist = distance.Distance(3)
making an object in the new file that denotes the module “distance” then adds the class, followed by the argument Distance(3)
now use it like you would from the module itself.
print(dist.miles)
FILES MUST BE IN THE SAME DIRECTORY
What is a package?
Create one. Remember, we have everything we need except one file to make it easier.
In the directory with your current project add the files you want as a part of your package into a folder called “calculations”.
Add all your python files to this.
Next, add a file called __init__.py
import everything using this file:
from .distance import *
from .area import *
from .timecalc import *
On your current project:
import calculations
object = calculations.Distance(3)
print(object.km)
Create a variable with the string “Hello, World”
Create an if statement that says if World isn’t in the equal to -1 then to do something. (Also, what number would pop up for world?)
inside the if statement replace “World” with “Reader”
create a string
a = “Hello, World”
search for world in the string
if a.find(“World”) != -1:
# Replace "World with Reader" b = a.replace("World", "Reader") # Display the results print(a) print("... was replaced with ...") print(b)
You could also just do print(a.replace(“World”, “Reader”))
Create the variable “title” with comprised of the string “Python Quick Start Guide”
Show it in all uppercase and then all lower case.
title = “Python Quick Start Guide”
print(title.upper())
print(title.lower())
create a variable and string combination:
tongue_twister = “She sells sea shells by the seashore.”
Show the count of every time an “s” was displayed.
print(tongue_twister.count(s))