-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReviewClass.py
More file actions
70 lines (49 loc) · 1.87 KB
/
ReviewClass.py
File metadata and controls
70 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# class contruction review
# defined the class
class Dog():
# all attributes of the doggy class
# methods and variables
# init sets a universal variable that can be changed in further methods
# init creates a universal variable of sorts
# init doesn't need to take something in (other than self) but sometimes
# you do want it to take in something
# def __init__(self): is empty
# name_input needs to be added to every time Dog is called/referenced
def __init__(self, name_input):
# "self" is here to set it in the namespace for an instance
self.age = 0
# assigning the dog's name to a class vairable
self.name = name_input
# all class methods must take "self" as the 0th input
def bark(self):
print("Woof woof!")
# this calls the name specifically and will print Luna (in this ex)
print(self.name + ", what do you see?")
# method to age the dog ;-;
# class methods always take in self at the 0th
def birthday(self):
# method to modify the age
print("Happy birthday " + self.name + "!")
self.age += 1
if(self.age == 1):
print(self.name + " is 1 year old!")
else:
print(self.name + " is " + str(self.age) + " years old!")
return self.age
def introduce(self):
print("Hi, my name is " + self.name + " and I am "
+ str(self.age) + " years old!")
Luna_ = Dog("Luna")
# new instance of the dog class, saved under variable name Luna
# Luna_.bark()
# calls bark from the dog class, only because Luna now = dog
# self takes Luna as the 0th input over self, which "instance" of the class are
# we walking about
# accessing dog's age
Luna_.age
Luna_.introduce()
Luna_.bark()
Luna_.bark()
Luna_.birthday()
Luna_.birthday()
#look at the namespace for this particular dog and its age