-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterminology.py
270 lines (186 loc) · 8.38 KB
/
terminology.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'''Miscellaneous Terms'''
# Literal
# -----------------------------------------------------------------------------
# a literal lets you assign values of an object in a single assignment,
# for example:
# assign a string literal to message
message = 'some information'
# assign an int literal to x
x = 100
# assign a dictionary literal to plants
plants = {
'spider': 'long, slender leaves',
'succulent': 'like a cactus',
'fern': 'prefers the forest',
}
# None
# -----------------------------------------------------------------------------
# None is not the same as False. Though it may look false when evaluated as a
# boolean, None is technically none as seen here:
def is_none(thing):
if thing is None:
print("it's None")
elif thing:
print("it's True")
else:
print("it's False")
thing = None
is_none(thing)
# it's None
thing = 0
is_none(thing)
# it's False
# Keyword arguments: **kwargs
# -----------------------------------------------------------------------------
# The keyword argument **h extracts the keys and values from a dictionary
# and supplies them as arguments to the class Element()
class Element:
def __init__(self, name, symbol, number):
self.name = name
self.symbol = symbol
self.number = number
h = {'name': 'Hydrogen', 'symbol': 'H', 'number': 1}
# these two produce the same result:
hydrogen = Element(name='Hydrogen', symbol='H', number=1)
hydrogen = Element(**h)
print(hydrogen.symbol)
# H
# Function
# -----------------------------------------------------------------------------
# A Function is a bit of reusable code that can de called after being defined.
# Functions can be defined by you in your code or they can be defined in
# libraries that are imported.
# Module
# -----------------------------------------------------------------------------
# Another word for library. Modules are simply python files that can be
# imported and used.
# Package
# -----------------------------------------------------------------------------
# A python module which can contain submodules. Essentially this is just a way
# of organizing many modules (files) into file hierarchies (folders).
# When you create a directory for your modules, you also create an empty file
# in that folder called: __init__.py. This file tells python to use the that
# directory name for importing modules within. For example:
'''
from myfolder import myfile
from myfolder.myfile import myfunction, myClass
'''
# Parameter
# -----------------------------------------------------------------------------
# A parameter is a named entity in a functions definition. Parameters can be
# replaced with arguments when the function is called.
# Argument
# -----------------------------------------------------------------------------
# A value passed to a function when calling the function. Arguments are
# assigned to the named local variables (parameters) in the functions body.
# Class
# -----------------------------------------------------------------------------
# A blueprint or template for creating an object. Classes are generally used
# when you need objects to have both data (attributes) and behaviour (methods).
# Object
# -----------------------------------------------------------------------------
# Everything in Python is an object. The official definition is: any data with
# state (attributes or value) and defined behavior (methods).
# Instance
# -----------------------------------------------------------------------------
# What you get when you create an object from a class.
# Attribute (also called an instance variable)
# -----------------------------------------------------------------------------
# A value associated with an object which is referenced by name using dot
# notation. For example:
class Person:
def __init__(self, name):
self.name = name
self.alive = True
snape = Person('Severus Snape')
# reference the name attribute:
snape.name
# Class variable (as opposed to instance variable: attribute)
# -----------------------------------------------------------------------------
# A value defined within the class, not within a classes method:
class Person:
homo = 'Sapien'
def __init__(self, name):
self.name = name
self.alive = True
snape = Person('Severus Snape')
# reference the class variable homo:
snape.homo
# Property
# -----------------------------------------------------------------------------
# Property is actually a method that returns a property attribute. It makes a
# method (a function in a class) behave like an attribute. It can be used like
# this: property() or as a decorator as: @property
# self
# -----------------------------------------------------------------------------
# Inside a class definition, self is a variable name used to signify something
# will belong to an instance of a that class. In other words, whenever a new
# instance of a class is created, "self" indicates that it will have its own
# variables that aren't shared with other instances.
# Method
# -----------------------------------------------------------------------------
# A function that is defined inside of a class. It can be called 'on' an
# instance of that class if self was included as its first argument.
class Person:
def __init__(self, name):
self.name = name
self.alive = True
def deceased(self):
self.alive = False
snape = Person('Severus Snape')
# call the method (function) like this:
snape.deceased()
# or like this:
Person.deceased(snape)
# Inheritance
# -----------------------------------------------------------------------------
# The idea that when one class is created from another, it will inherit that
# classes traits, unless specifically overwritten in its own definition.
# Composition
# -----------------------------------------------------------------------------
# A class can be composed of other classes as parts. A lengthier description:
# Composition is a way of aggregating objects together by making some objects
# attributes of other objects.
# Aggregation
# -----------------------------------------------------------------------------
# The distinction between aggregation and composition can blur slightly. Unlike
# composition, aggregation uses existing instances of objects to build up
# another object. The composed object does not actually own the objects that
# it's composed of. If it's destroyed, those objects continue to exist because
# they were instantiated outside of the class.
# "is a"
# -----------------------------------------------------------------------------
# A phrase to say something inherits from another.
# "has a"
# -----------------------------------------------------------------------------
# A phrase to say something is composed of others.
# Encapsulation
# -----------------------------------------------------------------------------
# Encapsulation is the idea that data inside an object should only be accessed
# through a public interface – that is, the object’s methods. Generally
# speaking encapsulation is the mechanism for restricting the access to some
# of an object's components. This means that the internal representation of an
# object can't be seen from outside of the objects definition. Access to this
# data is typically only achieved through methods.
# Delegation
# -----------------------------------------------------------------------------
# Delegation is an object oriented technique (also called a design pattern)
# where certain operations on one object are automatically applied to another,
# usually contained, object.
# Polymorphism
# -----------------------------------------------------------------------------
# Polymorphism is the idea that even though different types of objects can have
# very different traits (for example int objects can be divided whereas string
# objects can be capitalized), they can also have shared behaviors (for example,
# they are all printable):
int.__str__(0)
float.__str__(0.0)
list.__str__(0)
tuple.__str__(0)
# So polymorphism basically means that objects can be more than one thing at
# the same time. Inheritance is one way to implement polymorphism. In the above
# example, every type of object automatically inherits the __str__ method from
# its object base class.
# Another way of implementing polymorphism: one class can behave like another
# class as long as it contains the necessary methods and data attributes. This
# Is often called duck typing.