10000 review and rename · coderwxy/book1-exercises@255e470 · GitHub
[go: up one dir, main page]

Skip to content

Commit 255e470

Browse files
committed
review and rename
1 parent dff1e58 commit 255e470

File tree

10 files changed

+439
-0
lines changed

10 files changed

+439
-0
lines changed

refactor/chp21/dog_class.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Dog():
2+
3+
# Class Attribute
4+
species = 'mammal'
5+
6+
# Initializer / Instance Attributes
7+
def __init__(self, name, age):
8+
self.name = name
9+
self.age = age
10+
11+
12+
# Instantiate the Dog object
13+
philo = Dog("Philo", 5)
14+
mikey = Dog("Mikey", 6)
15+
16+
# Access the instance attributes
17+
print("{} is {} and {} is {}.".format(
18+
philo.name, philo.age, mikey.name, mikey.age))
19+
20+
# Is Philo a mammal?
21+
if philo.species == "mammal":
22+
print("{0} is a {1}!".format(philo.name, philo.species))

refactor/chp21/dog_inheritence.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Parent class
2+
class Dog():
3+
4+
# Class attribute
5+
species = 'mammal'
6+
7+
# Initializer / Instance attributes
8+
def __init__(self, name, age):
9+
self.name = name
10+
self.age = age
11+
12+
# instance method
13+
def description(self):
14+
return self.name, self.age
15+
16+
# instance method
17+
def speak(self, sound):
18+
return "%s says %s" % (self.name, sound)
19+
20+
21+
# child class (inherits from Dog() class)
22+
class RussellTerrier(Dog):
23+
def run(self, speed):
24+
return "%s runs %s" % (self.name, speed)
25+
26+
27+
# child class (inherits from Dog() class)
28+
class Bulldog(Dog):
29+
def run(self, speed):
30+
return "%s runs %s" % (self.name, speed)
31+
32+
33+
# child classes inherit attributes and
34+
# behaviors from the parent class
35+
jim = Bulldog("Jim", 12)
36+
print(jim.description())
37+
38+
# child classes have specific attributes
39+
# and behaviors as well
40+
print(jim.run("slow"))
Lines changed: 24 additions & 0 deletions
< F438 tbody>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Dog():
2+
3+
# Class Attribute
4+
species = 'mammal'
5+
6+
# Initializer / Instance Attributes
7+
def __init__(self, name, age):
8+
self.name = name
9+
self.age = age
10+
11+
# instance method
12+
def description(self):
13+
return self.name, self.age
14+
15+
# instance method
16+
def speak(self, sound):
17+
return "%s says %s" % (self.name, sound)
18+
19+
# Instantiate the Dog object
20+
mikey = Dog("Mikey", 6)
21+
22+
# call our instance methods
23+
print(mikey.description())
24+
print(mikey.speak("Gruff Gruff"))

refactor/chp21/dog_isinstance.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Parent class
2+
class Dog():
3+
4+
# Class attribute
5+
species = 'mammal'
6+
7+
# Initializer / Instance attributes
8+
def __init__(self, name, age):
9+
self.name = name
10+
self.age = age
11+
12+
# instance method
13+
def description(self):
14+
return self.name, self.age
15+
16+
# instance method
17+
def speak(self, sound):
18+
return "%s says %s" % (self.name, sound)
19+
20+
21+
# child class (inherits from Dog() class)
22+
class RussellTerrier(Dog):
23+
def run(self, speed):
24+
return "%s runs %s" % (self.name, speed)
25+
26+
27+
# child class (inherits from Dog() class)
28+
class Bulldog(Dog):
29+
def run(self, speed):
30+
return "%s runs %s" % (self.name, speed)
31+
32+
33+
# child classes inherit attributes and
34+
# behaviors from the parent class
35+
jim = Bulldog("Jim", 12)
36+
print(jim.description())
37+
38+
# child classes have specific attributes
39+
# and behaviors as well
40+
print(jim.run("slow"))
41+
42+
# is jim an instance of Dog()?
43+
print(isinstance(jim, Dog))
44+
45+
# is julie an instance of Dog()?
46+
julie = Dog("Julie", 100)
47+
print(isinstance(julie, Dog))
48+
49+
# is johnny walker an instance of Bulldog()
50+
johnnywalker = RussellTerrier("Johnny Walker", 4)
51+
print(isinstance(johnnywalker, Bulldog))
52+
53+
# is julie and instance of jim?
54+
print(isinstance(julie, jim))

refactor/chp21/dog_walking.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# parent class
2+
class Pet():
3+
4+
dogs = []
5+
6+
def __init__(self, dogs):
7+
self.dogs = dogs
8+
9+
def walk(self):
10+
for dog in self.dogs:
11+
print(dog.walk())
12+
13+
14+
# parent class
15+
class Dog():
16+
17+
# class attribute
18+
species = 'mammal'
19+
is_hungry = True
20+
21+
# initializer / instance attributes
22+
def __init__(self, name, age):
23+
self.name = name
24+
self.age = age
25+
26+
# instance method
27+
def description(self):
28+
return self.name, self.age
29+
30+
# instance method
31+
def speak(self, sound):
32+
return "%s says %s" % (self.name, sound)
33+
34+
# instance method
35+
def eat(self):
36+
self.is_hungry = False
37+
38+
def walk(self):
39+
return "%s is walking!" % (self.name)
40+
41+
42+
# child class (inherits from Dog() class)
43+
class RussellTerrier(Dog):
44+
def run(self, speed):
45+
return "%s runs %s" % (self.name, speed)
46+
47+
48+
# child class (inherits from Dog() class)
49+
class Bulldog(Dog):
50+
def run(self, speed):
51+
return "%s runs %s" % (self.name, speed)
52+
53+
# create instances of dogs
54+
my_dogs = [Bulldog("Tom", 6), RussellTerrier("Fletcher", 7), Dog("Larry", 9)]
55+
56+
# instantiate the Pet() class
57+
my_pets = Pet(my_dogs)
58+
59+
# output
60+
my_pets.walk()

refactor/chp21/github_with_class.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from urllib.request import Request, urlopen
2+
3+
4+
class Github(object):
5+
6+
def __init__(self, username):
7+
self.username = username
8+
self.base_url = 'https://api.github.com/users/'
9+
10+
def get_user_info(self):
11+
full_url = "{0}{1}".format(self.base_url, self.username)
12+
request = Request(full_url)
13+
handler = urlopen(request)
14+
if handler.getcode() == 200:
15+
return handler.read()
16+
17+
def get_user_repos(self):
18+
full_url = "{0}{1}/repos".format(self.base_url, self.username)
19+
request = Request(full_url)
20+
handler = urlopen(request)
21+
if handler.getcode() == 200:
22+
return handler.read()
23+
24+
25+
user = Github('mjhea0')
26+
print(user.get_user_info())
27+
print(user.get_user_repos())

refactor/chp21/my_farm.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class Animal():
2+
3+
# class attributes
4+
stuff_in_belly = 0
5+
position = 0
6+
7+
# initializer
8+
def __init__(self, name, color):
9+
self.name = name
10+
self.color = color
11+
12+
# instance methods
13+
def talk(self):
14+
return "Hello. I'm {}".format(self.name)
15+
16+
def walk(self, walk_increment):
17+
self.position += walk_increment
18+
return self.position
19+
20+
def run(self, run_increment):
21+
self.position += run_increment
22+
return self.position
23+
24+
def feed(self):
25+
self.stuff_in_belly += 1
26+
if self.stuff_in_belly > 3:
27+
return self.poop()
28+
else:
29+
return "{} is eating.".format(self.name)
30+
31+
def hungry(self):
32+
if self.stuff_in_belly < 2:
33+
return "{} is hungry".format(self.name)
34+
else:
35+
return "{} is not hungry".format(self.name)
36+
37+
def poop(self):
38+
self.stuff_in_belly == 0
39+
return "Ate too much ... need to find a bathroom"
40+
41+
42+
class Dog(Animal):
43+
44+
def talk(self):
45+
return "Bark! Bark!"
46+
47+
def fetch(self):
48+
return "{} is fetching.".format(self.name)
49+
50+
51+
class Sheep(Animal):
52+
53+
def talk(self):
54+
return "Baaa Baaa"
55+
56+
57+
class Pig(Animal):
58+
59+
def talk(self):
60+
return "Oink Oink"
61+
62+
63+
# create a dog
64+
dog = Dog("Blitzer", "yellow")
65+
# output the dog's attributes
66+
print("Our dog's name is {}.".format(dog.name))
67+
print("And he's {}.".format(dog.color))
68+
# output some behavior
69+
print("Say something, {}.".format(dog.name))
70+
print(dog.talk())
71+
print("Go fetch!")
72+
print(dog.fetch())
73+
# walk the dog
74+
print("{} is at position {}.".format(dog.name, dog.walk(2)))
75+
# run the dog
76+
print("{} is now at position {}".format(dog.name, dog.run(4)))
77+
# feed the dog
78+
print(dog.feed())
79+
# is the dog hungry
80+
print(dog.hungry())
81+
# feed the dog more
82+
print(dog.feed())
83+
print(dog.feed())
84+
print(dog.hungry())
85+
print(dog.feed())
86+
87+
print("\n")
88+
89+
sheep = Sheep("Shaun", "white")
90+
print(sheep.talk())
91+
print(sheep.run(2))
92+
print(sheep.run(2))
93+
94+
print("\n")
95+
96+
pig = Pig("Carl", "pink")
97+
print(pig.talk())

refactor/chp21/oldest_dog.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Dog():
2+
3+
# Class Attribute
4+
species = 'mammal'
5+
6+
# Initializer / Instance Attributes
7+
def __init__(self, name, age):
8+
self.name = name
9+
self.age = age
10+
11+
12+
# Instantiate the Dog object
13+
jake = Dog("Jake", 7)
14+
doug = Dog("Doug", 4)
15+
william = Dog("William", 5)
16+
17+
18+
# Determine the oldest dog
19+
def get_biggest_number(*args):
20+
return max(args)
21+
22+
# output
23+
print("The oldest dog is {} years old.".format(
24+
get_biggest_number(jake.age, doug.age, william.age)))

refactor/chp21/oop_comp_check.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Questions
2+
3+
1. What's a class?
4+
2. What's an instance?
5+
3. What's the relationship between a class and an instance?
6+
4. What's the Python syntax used for defining a new class?
7+
5. What's the spelling convention for a class name?
8+
6. How do you instantiate, or create an instance of, a class?
9+
7. How do you access the attributes and behaviors of a class instance?
10+
8. What's a method?
11+
9. What's the purpose of `self`?
12+
10. What's the purpose of the `__init__` method?
13+
11. Describe how inheritance helps prevent code duplication.
14+
12. Can child classes override properties of their parents?
15+
16+
## Answers
17+
18+
1. A class is a mechanism used to create new user-defined data structures. It contains data as well as the methods used to process that data.
19+
2. An instance is a copy of the class with *actual* values, literally an object of a specific class.
20+
3. While a class is a blueprint used to describe how to make something, instances are objects created from those blueprints.
21+
4. `class PythonClassName():`
22+
5. CamelCase notation, starting with a capital letter - i.e., `PythonClassName()`
23+
6. You use the the class name, followed by parentheses. So if the class name is `Dog()`, an dog instance would be - `my_class = Dog()`.
24+
7. With dot notation - e.g., `instance_name.attribute_name`
25+
8. A function that's defined inside a class.
26+
9. The first argument of every method references the current instance of the class, which by convention, is named `self`. In the `__init__` method, `self` refers to the newly created object; while in other methods, `self` refers to the instance whose method was called. For more on `__init__` vs. `self`, check out [this](http://stackoverflow.com/a/625098) article.
27+
10. The `__init__` method initializes an instance of a class.
28+
11. Child classes inherit all of the parent's attributes and behaviors.
29+
12. Yes.
30+

0 commit comments

Comments
 (0)
0