-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOOP_and_Classes.py
64 lines (48 loc) · 2.63 KB
/
OOP_and_Classes.py
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
# Class: Template for creating objects. All objects created using the same class will have the same characteristcs.
# Object: an instance of a class.
# Instantiate: Create an instance of a class.
# Method: A function defined in a class.
# Attribute: a variable bound to an instance of a class
# Self: reference to the instance of a class
# Constructor: refers to a special method that is executed when an instance of a class is created/constructed
# Imperative programming involves providing a computer
# with a series of instruction that are in a defined order.
# Object Oriented programming combines data and the processes
# that work on that data into objects. This is also known as encapsulation
# Object Oriented programming makes use of imperative programming
# in the methods that object use to manipulate their data.
# Performing tasks using an OOP approach uses less code.
# "Python Supports multiple programming paradigms, including object-oriented,
# imperative, and fucntional programming or procedural styles."
# https://en.wikipedia.org/wiki/Python_(programming_language)
import webbrowser
webbrowser.open('https://en.wikipedia.org/wiki/Python_(programming_language)')
# In this case, 'webbrowser' is an object, and '.open' is a method thats
# included in the webbrowser object
a = 12
b = 4
print(a + b)
print(a.__add__(b)) # _add_ is the method here
# A class can be considered a template from which objects can be created
class Kettle(object):
def __init__(self, make, price): # initializes class using the __init__Constructor
self.make = make
self.price = price
self.on = False
# The code below creates an instance of the kettle class called "kenwood"
# Each instance of the kettle class has its own name and price
# Each make and price instace of the kettle class can be accessed by
# using dot notation.
# if i wanted to access the price of the kenwood instance I would put
# kenwood.price.
kenwood = Kettle("Kenwood", 8.99) # Here, an object of type Kettle is initialized and called "Kenwood" with a price of 8.99
print(kenwood.make) # This happened because the make and price attributes were initialized in the def __init__ code bock
print(kenwood.price)
kenwood.price = 12.75 # Adjusting price
print(kenwood.price)
hamilton = Kettle("Hamilton", 14.55)
print("Models: {} = {}, {} = {}".format(kenwood.make, kenwood.price,
hamilton.make, hamilton.price))
# When a variable is band to an instance of a class is is considered
# a data attribute. In the case above, make and price in the Kettle
# class are attributes.