0% found this document useful (0 votes)
36 views

Python Tutorial - Classes and Instances - 2020

Uploaded by

Linda Takuva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Python Tutorial - Classes and Instances - 2020

Uploaded by

Linda Takuva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

BogoToBogo HOME ABOUT BIG DATA MACHINE LEARNING ANGULARJS PYTHON C++ GO DEVOPS

KUBERNETES ALGORITHMS MORE...

PYTHON - CLASSES AND INSTANCES (__INIT__, Ph.D. / Golden Gate Ave, San Francisco / Seoul
National Univ / Carnegie Mellon / UC Berkeley /

__CALL__, ETC.) DevOps / Deep Learning / Visualization

Sponsor Open Source development activities and


free contents for everyone.

Thank you.

- K Hong

bogotobogo.com site search:


Custom Search Search

Classes and Instances


Unlike C++, classes in Python are objects in their own right, even without instances. They are
just self-contained namespaces. Therefore, as long as we have a reference to a class, we can
set or change its attributes anytime we want.

Python
tutorial
De ning Classes
The following statement makes a class with no attributes attached, and in fact, it's an empty
namespace object: Python Home

Introduction
class Student:
pass Running Python Programs
(os, sys, import)
The name of this class is Student, and it doesn't inherit from any other class. Class names are
Modules and IDLE (Import,
usually capitalized, but this is only a convention, not a requirement. Everything in a class is
Reload, exec)
indented, just like the code within a function, if statement, for loop, or any other block of code.
The rst line not indented is outside the class. Object Types - Numbers,
Strings, and None
In the code, the pass is the no-operation statement. This Student class doesn't de ne any
methods or attributes, but syntactically, there needs to be something in the de nition, thus Strings - Escape Sequence,
the pass statement. This is a Python reserved word that just means move along, nothing to Raw String, and Slicing
see here. It's a statement that does nothing, and it's a good placeholder when we're stubbing
Strings - Methods
out functions or classes. The pass statement in Python is like an empty set of curly braces {} in
Java or C.
Formatting Strings -
expressions and method
calls
>>> Student.name = "Jack"
>>> Student.id = 20001
Files and os.path

Then, we attached attributes to the class by assigning name to it outside of the class. In this
Traversing directories
case, the class is basically an objec with eld names attached to it.
recursively

>>> print(Student.name)
Subprocess Module
Jack

Regular Expressions with


Note that this is working even though there are no instances of the class yet. Python

Object Types - Lists

Object Types - Dictionaries


The __init__() method and Tuples

Functions def, *args, **kargs


Many classes are inherited from other classes, but the one in the example is not. Many classes
de ne methods, but this one does not. There is nothing that a Python class absolutely must
Functions lambda
have, other than a name. In particular, C++ programmers may nd it odd that Python classes
don't have explicit constructors and destructors. Although it's not required, Python classes can Built-in Functions
have something similar to a constructor: the __init__() method.
map, lter, and reduce
In Python, objects are created in two steps:
Decorators

1. Constructs an object
List Comprehension
__new()__
2. Initializes the object Sets (union/intersection) and
__init()__ itertools - Jaccard coe cient
and shingling to check
However, it's very rare to actually need to implement __new()__ because Python constructs our plagiarism

objects for us. So, in most of the cases, we usually only implement the special method,
Hashing (Hash tables and
__init()__.
hashlib)

Let's create a class that stores a string and a number: Dictionary Comprehension
with zip

The yield keyword

Generator Functions and


Expressions

generator.send() method

Iterators

Classes and Instances


(__init__, __call__, etc.)
class Student(object):
'''Classes can (and should) have docstrings too, just like modules and functi if__name__ == '__main__'
def __init__(self, name, id = 20001):
self.name = name argparse
self.id = id

Exceptions

When a def appears inside a class, it is usually known as a method. It automatically receives a @static method vs class
special rst argument, self, that provides a handle back to the instance to be processed. method
Methods with two underscores at the start and end of names are special methods.
Private attributes and
The __init__() method is called immediately after an instance of the class is created. It would be private methods
tempting to call this the constructor of the class. It's really tempting, because it looks like a
bits, bytes, bitstring, and
C++ constructor, and by convention, the __init__() method is the rst method de ned for the
constBitStream
class. It appears to be acting like a constructor because it's the rst piece of code executed in a
newly created instance of the class. However, it's not like a constructor, because the object has json.dump(s) and
already been constructed by the time the __init()__ method is called, and we already have a json.load(s)
valid reference to the new instance of the class.
Python Object Serialization -
The rst parameter of __init()__ method, self, is equivalent to this of C++. Though we do not pickle and json
have to pass it since Python will do it for us, we must put self as the rst parameter of
Python Object Serialization -
nonstatic methods. But the self is always explicit in Python to make attribute access more
yaml and json
obvious.
Priority queue and heap
queue data structure

Graph data structure


Find Your
Dijkstra's shortest path
Soulmate algorithm

Prim's spanning tree


High Quality Dating Chat algorithm
Dating.com
Closure
The self is always a reference to the current instance of the class. Though this argument lls
the role of the reserved word this in c++ or Java, but self is not a reserved word in Python, Functional programming in
Python
merely a naming convention. Nonetheless, please don't call it anything but self; this is a very
strong convention.
Remote running a local le
using ssh
When a method assigns to a self attribute, it creates an attribute in an instance because self
refers to the instance being processed. SQLite 3 - A. Connecting to
DB, create/drop table, and
insert data into a table

SQLite 3 - B. Selecting,
Instantiating classes updating and deleting data

MongoDB with PyMongo I -


To instantiate a class, simply call the class as if it were a function, passing the arguments that
Installing MongoDB ...
the __init__() method requires. The return value will be the newly created object. In Python,
there is no explicit new operator like there is in c++ or Java. So, we simply call a class as if it Python HTTP Web Services -
were a function to create a new instance of the class: urllib, httplib2

Web scraping with Selenium


s = Student(args) for checking domain
availability
We are creating an instance of the Student class and assigning the newly created instance to
REST API : Http Requests for
the variable s. We are passing one parameter, args, which will end up as the argument in
Humans with Flask
Student's __init__() method.

Blog app with Tornado


s is now an instance of the Student class. Every class instance has a built-in attribute,
__class__, which is the object's class. Java programmers may be familiar with the Class class, Multithreading ...
which contains methods like getName() and getSuperclass() to get metadata information
about an object. In Python, this kind of metadata is available through attributes, but the idea is Python Network
the same. Programming I - Basic Server
/ Client : A Basics

We can access the instance's docstring just as with a function or a module. All instances of a
Python Network
class share the same docstring. Programming I - Basic Server
/ Client : B File Transfer

Python Network
Find Your Programming II - Chat Server
/ Client
Soulmate
Python Network
Meet Charming Singles Programming III - Echo
Server using socketserver
and Get More Memorable
network framework
Dating.com Moments
Python Network
We can use the Student class de ned above as following: Programming IV -
Asynchronous Request
Handling : ThreadingMixIn
studentA = Student("Jack") and ForkingMixIn
studentB = Student("Judy", 10005)
Python Interview Questions I
Unlike C++, the attributes of Python object are public, we can access them using the dot(.)
Python Interview Questions
operator:
II

>>>studentA.name
Python Interview Questions
'Jack' III
>>>studentB.id
10005 Python Interview Questions
IV
We can also assign a new value to the attribute:
Python Interview Questions
V
>>> studentB.id = 80001
>>> studentB.id Python Interview Questions
80001 VI

How about the object destruction? Python Interview Questions


Python has automatic garbage collection. Actually, when an object is about to be garbage- VII
collected, its __del()__ method is called, with self as its only argument. But we rarely use this
Image processing with
method.
Python image library Pillow

Python and C++ with SIP

Instance vairables PyDev with Eclipse

Matplotlib
Let's look at another example:
Redis with Python

class Student: NumPy array basics A


def __init__(self, id):
self.id = id NumPy Matrix and Linear
def setData(self, value):
Algebra
self.data = value
def display(self):
print(self.data) Pandas with NumPy and
Matplotlib

What is self.id?
Celluar Automata
It's an instance variable. It is completely separate from id, which was passed into the
__init__() method as an argument. self.id is global to the instance. That means that we can Batch gradient descent
access it from other methods. Instance variables are speci c to one instance of a class. For algorithm
example, if we create two Student instances with di erent id values, they will each remember
their own values. Longest Common Substring
Algorithm
Then, let's make two instances:
Python Unit Test - TDD using
unittest.TestCase class
>>> s1 = Student()
>>> s2 = Student() Simple tool - Google page
ranking by keywords

Here, we generated instance objects. These objects are just namespaces that have access to
Google App Hello World
their classes' attributes. The two instances have links back to the class from which they were
created. If we use an instance with the name of an attribute of class object, Python retrieves Google App webapp2 and
the name from the class. WSGI

Uploading Google App Hello


>>> s1.setData("Jake") # method call, self is s1
World
>>> s2.setData(4294967296) # runs Student.setData(s2,4294967296)

Python 2 vs Python 3

virtualenv and
Math Video Courses virtualenvwrapper

- $20 Sale Uploading a big le to AWS


S3 using boto module
Learn Fast by Solving Problems -
Algebra | Trig | Calculus | Physics | Scheduled stopping and
Statistics & More. starting an AWS instance
MathAndScience.com
Cloudera CDH5 - Scheduled
Note that neither s1 nor s2 has a setData() attribute of its own. So, Python follows the link stopping and starting
from instance to class to nd the attribute. services

Removing Cloud Files -


In the setData() function inside Student, the value passed in is assigned to self.data. Within a
Rackspace API with curl and
method, self automatically refers to the instance being processed (s1 or s2). Thus, the
subprocess
assignment store values in the instances' namespaces, not the class's.
Checking if a process is
When we call the class's display() method to print self.data, we see the self.data di ers in running/hanging and
each instance. But the name display() itself is the same in s1 and s2: stop/run a scheduled task on
Windows

>>> s1.display() Apache Spark 1.3 with


Jake PySpark (Spark Python API)
>>> s2.display() Shell
4294967294
>>>
Apache Spark 1.2 Streaming

Note that we stored di erent object types in the data member in each instance. In Python, bottle 0.12.7 - Fast and
there are no declarations for instance attributes (members). They come into existence when simple WSGI-micro
they are assigned values. The attribute named data does not even exist in memory until it is framework for small web-
assigned within the setData() method. applications ...

Flask app with Apache WSGI


on Ubuntu14/CentOS7 ...

Selenium WebDriver

Fabric - streamlining the use


example A of SSH for application
deployment
class MyClassA:
def setData(self,d): Ansible Quick Preview -
self.data = d Setting up web servers with
def display(self):
Nginx, con gure
print(self.data)
enviroments, and deploy an
App
Then, we generate instance objects:
Neural Networks with
backpropagation for XOR
a = MyClassA()
using one hidden layer
a2 = MyClassA()

NLP - NLTK (Natural


The instance objects are just namespaces that have access to their classes' attributes. Actually, Language Toolkit) ...
at this point, we have three objects: a class and two instances. Note that neither a nor a2 has a
setData attribute of its own. However, the value passed into the setData is assigned to RabbitMQ(Message broker
self.data. Within a method, self automatically refers to the instance being processed (a or a2). server) and Celery(Task
So, the assignments store values in the instances' namespaces, not the class's. Methods must queue) ...

go through the self argument to get the instance to be processed. We can see it from the
OpenCV3 and Matplotlib ...
output:

Simple tool - Concatenating


slides using FFmpeg ...
>>> a.setData("Python")
>>> a2.setData("PyQt")
>>> a.display() iPython - Signal Processing
Python with NumPy
>>> a2.display()
PyQt
iPython and Jupyter - Install
>>>
Jupyter, iPython Notebook,
drawing with Matplotlib, and
As we expected, we stored the value for each instance object even though we used the same publishing it to Github
method, display. The self made all the di erences! It refers to instances.
iPython and Jupyter
Notebook with Embedded
D3.js

Downloading YouTube
example B videos using youtube-dl
Superclass is listed in parenthesis in a class header as we see the example below. embedded with Python

Machine Learning : scikit-


class MyClassB(MyClassA): learn ...
def display(self):
print('B: data = "%s"' %self.data) Django 1.6/1.8 Web
Framework ...
MyClassB rede nes the display of its superclass, and it replaces the display attribute while
still inherits the setData method in MyClassA as we see below:

>>> b = MyClassB()
>>> b.setData("BigData") Sponsor Open Source development activities and
free contents for everyone.
>>> b.display()
B: data = "BigData"

Thank you.
But for the instance of MyClassA is still using the display, previously de ned in MyClassA.
- K Hong

>>> a2.display()
PyQt
>>>

example C
Operator overloading allows objects intercepts and respond to operations. It makes object
interfaces more consistent and it also allows class objects to act like built-ins

class MyClassC(MyClassB):
def __init__(self, d):
self.data = d
def __add__(self,d2):
return MyClassC(self.data + d2)
def __str__(self):
return '[MyClassC: %s]' % self.data
def mul(self, d2):
self.data *= d2

MyClassC is a MyClassB, and its instances inherits the display() method from MyClassB.

>>> c = MyClassC("123")
>>> c.display()
B: data = "123"
>>> print(c)
[MyClassC: 123]
>>> c = c + '789'
>>> c.display() OpenCV 3
B: data = "123789"
>>> print(c) image and
[MyClassC: 123789]
>>> c = MyClassC('123') video
processing
>>> c.mul(3)
>>> print c

with Python
[MyClassC: 123123123]

When MyClassC is created, an argument '123' is passed. This is passed to the d argument in
the __init__ constructor and assigned to self.data. Actually, MyClassC arranges to set the data
attribute automatically at construction time. It does not require calling setdata() from OpenCV 3 with Python
instance at later time.
Image - OpenCV BGR :
Matplotlib RGB
For +, Python passes the instance object on the left of the self argument in __add__ and the
value on the right to d2. For print(), Python passes the object being printed to self in __str__. Basic image operations -
Som whatever string this method returns is taken to be the print string for the object. By pixel access
implementing __str__, we can use print to display objects of this class, and we do not have to
call the display() method. iPython - Signal Processing
with NumPy

The __add__ makes and returns a new instance object by calling MyClassC. However, mul
Signal Processing with
changes the current instance object in-place by reassigning the self attribute.
NumPy I - FFT and DFT for
sine, square waves,
unitpulse, and random
signal

Methods Signal Processing with


NumPy II - Image Fourier
Here is an example of a class Rectangle with a member function returning its area. Transform : FFT & DFT

Inverse Fourier Transform of


class Rectangle(object): an Image with low pass lter:
def __init__(self, w, h): cv2.idft()
self.width = w
self.height = h
Image Histogram
def area(self):
return self.width * self.height
Video Capture and Switching
colorspaces - RGB / HSV
>>> rect = Rectangle(100,20)
>>> rect.area() Adaptive Thresholding -
2000
Otsu's clustering-based
>>> rect.height = 30
>>> rect.area() image thresholding
3000
>>> Edge Detection - Sobel and
Laplacian Kernels
Note that this version is using direct attribute access for the width and height.
Canny Edge Detection

We could have used the following implementing setter and getter methods:
Hough Transform - Circles

Watershed Algorithm :
class Rectangle(object):
def __init__(self, w, h):
Marker-based Segmentation
self.width = w I
self.height = h
def getWidth(self): Watershed Algorithm :
return self.width
Marker-based Segmentation
def getHeight(self):
return self.height
II
def setWidth(self, w):
self.width = w Image noise reduction : Non-
def setHeight(self): local Means denoising
self.height = h algorithm
def area(self):
return self.getWidth() * self.getHeight()
Image object detection : Face
detection using Haar
Object attributes are where we store our information, and most of the case the following Cascade Classi ers
syntax is enough:
Image segmentation -
Foreground extraction
objName.attribute # Retrieve attribute value Grabcut algorithm based on
objName.attribute = value # Change attribute value
graph cuts

However, there are cases when more exibility is required. For example, to validate the setter Image Reconstruction -
and getter methods, we may need to change the whole code like this: Inpainting (Interpolation) -
Fast Marching Methods

class Rectangle(object): Video : Mean shift object


... tracking
def getWidth(self):
if not valid():
raise TypeError('cannot retrieve width')
Machine Learning :
else: Clustering - K-Means
return self.width.convert() clustering I
...
def setWidth(self, w):
Machine Learning :
if not valid(w):
Clustering - K-Means
raise TypeError('cannot set width')
else: clustering II
self.width = convert(w)
... Machine Learning :
Classi cation - k-nearest
neighbors (k-NN) algorithm

Properties
Machine
The solution for the issue of exibility is to allow us to run code automatically on attribute
access, if needed. The properties allow us to route a speci c attribute access (attribute's get
and set operations) to functions or methods we provide, enabling us to insert code to be run
automatically. A property is created by assigning the result of a built-in function to a class
Learning
attribute: with scikit-
learn
attribute = property(fget, fset, fdel, doc)

We pass scikit-learn installation


1. fget: a function for intercepting attribute fetches
2. fset: a function for assignments scikit-learn : Features and
feature extraction - iris
3. fdel: a function for attribute deletions
dataset
4. doc: receives a documentation string for the attribute

scikit-learn : Machine
If we go back to the earlier code, and add property(), then the code looks like this: Learning Quick Preview

scikit-learn : Data
class Rectangle(object): Preprocessing I - Missing /
def __init__(self, w, h):
Categorical data
self.width = w
self.height = h
def area(self): scikit-learn : Data
return self.width * self.height Preprocessing II -
m_area = property(area) Partitioning a dataset /
Feature scaling / Feature
We can use the class as below: Selection / Regularization

scikit-learn : Data
>>> rect = Rectangle (33, 9) Preprocessing III -
>>> rect.width, rect.height, rect.m_area Dimensionality reduction vis
(33, 9, 297) Sequential feature selection
/ Assessing feature
The example above simply traces attribute accesses. However, properties usually do compute importance via random
the value of an attribute dynamically when fetched, as the following example illustrates: forests

Data Compression via


class SquareIt: Dimensionality Reduction I -
def __init__(self, init): Principal component
self.value = init analysis (PCA)
def getValue(self):
return self.value**2
def setValue(self, v): scikit-learn : Data
self.value = v Compression via
V = property(getValue, setValue) Dimensionality Reduction II -
Linear Discriminant Analysis
The class de nes an attribute V that is accessed as though it were static data. However, it (LDA)

really runs code to compute its value when fetched. When the code runs, the value is stored in
scikit-learn : Data
the instance as state information, but whenever we retrieve it via the managed attribute, its
Compression via
value is automatically squared. Dimensionality Reduction III
- Nonlinear mappings via
kernel principal component
>>> S1 = SquareIt(4)
(KPCA) analysis
>>> S1.value
4
>>> S1.V scikit-learn : Logistic
16 Regression, Over tting &
>>> S1.V = 5 regularization
>>> S1.V
25
>>> S2 = SquareIt(64) scikit-learn : Supervised
>>> S2.value Learning & Unsupervised
64 Learning - e.g. Unsupervised
>>> S2.V PCA dimensionality
4096
reduction with iris dataset
>>>

scikit-learn :
Again, note that the fetch computes the square of the instance's data. Unsupervised_Learning -
KMeans clustering with iris
dataset

scikit-learn : Linearly
Operator overloading: 2.6 __cmp__() Separable Data - Linear
Model & (Gaussian) radial
(Removed in 3.0) basis function kernel (RBF
kernel)
By implementing __cmp__() method, all of the comparison operators(<, ==, !=, >, etc.) will work.
scikit-learn : Decision Tree
Learning I - Entropy, Gini,
So, let's add the following to our Rectangle class: and Information Gain

scikit-learn : Decision Tree


class Rectangle(object):
Learning II - Constructing the
def __init__(self, w, h):
self.width = w Decision Tree
self.height = h
def area(self): scikit-learn : Random
return self.width * self.height Decision Forests
def __cmp__(self, other):
Classi cation
return cmp(self.area, other.area)

scikit-learn : Support Vector


Note that we used the built-in cmp() function to implement __cmp__. The cmp() function Machines (SVM)
returns -1 if the rst argument is less than the second, 0 if they are equal, and 1 if the rst
argument is greater than the second. scikit-learn : Support Vector
Machines (SVM) II
For Python 3.0, we get TypeError: unorderable types. So, we need to use speci c methods
Flask with Embedded
since the __cmp__() and cmp() built-in functions are removed in Python 3.0.
Machine Learning I :
Serializing with pickle and
DB setup

Flask with Embedded


Machine Learning II : Basic

Operator overloading: __str__ Flask App

Flask with Embedded


The __str__ is the 2nd most commonly used operator overloading in Python after __init__. The Machine Learning III :
__str__ is run automatically whenever an instance is converted to its print string. Embedding Classi er

Flask with Embedded


Let's use the previous example:
Machine Learning IV : Deploy

Flask with Embedded


class Rectangle(object):
def __init__(self, w, h):
Machine Learning V :
self.width = w Updating the classi er
self.height = h
def area(self): scikit-learn : Sample of a
return self.width * self.height
spam comment lter using
SVM - classifying a good one
If we print the instance, it displays the object as a whole as shown below. or a bad one

>>> r1 = Rectangle(100,20)
>>> print(r1)
<__main__.Rectangle object at 0x02B40750>

MACHINE LEARNING
It displays the object's class name, and its address in memory which is basically useless except ALGORITHMS AND
as a unique identi er.
CONCEPTS
So, let's add the __str__ method: Batch gradient descent
algorithm

class Rectangle(object):
Single Layer Neural Network
def __init__(self, w, h):
self.width = w
- Perceptron model on the
self.height = h Iris dataset using Heaviside
def area(self): step activation function
return self.width * self.height
def __str__(self): Batch gradient descent
return '(Rectangle: %s, %s)' %(self.width, self.height)
versus stochastic gradient
>>> r1 = Rectangle(100,20) descent
>>> print(r1)
(Rectangle: 100, 20] Single Layer Neural Network
- Adaptive Linear Neuron
The code above extends our class to give a custom display that lists attributes when our class's using linear (identity)
activation function with
instances are displayed as a whole, instead of relying on the less useful display. Note that
batch gradient descent
we're doing string % formatting to build the display string in __str__.
method

Single Layer Neural Network


: Adaptive Linear Neuron

__str__ vs __repr__ using linear (identity)


activation function with
stochastic gradient descent
The di erence between __str__ and __repr__ are not that obvious. (SGD)

Logistic Regression
When we use print, Python will look for an __str__ method in our class. If it nds one, it will call
it. If it does not, it will look for a __repr__ method and call it. If it cannot nd one, it will create
VC (Vapnik-Chervonenkis)
an internal representation of our object. Dimension and Shatter

Bias-variance tradeo
>>> class MyClass:
def __init__(self, d):
self.data = d
Maximum Likelihood
Estimation (MLE)
>>> x = MyClass('secret')
>>> print(x) Neural Networks with
<__main__.MyClass instance at 0x027335D0>
backpropagation for XOR
>>> x
using one hidden layer
<__main__.MyClass instance at 0x027335D0>
>>>
minHash

Not much information from print(x) and just echoing the object x. That's why we do
tf-idf weight
customize the class by using __str__.
Natural Language Processing
(NLP): Sentiment Analysis I
>>> class MyClass:
(IMDb & bag-of-words)
def __init__(self, d):
self.data = d
def __str__(self): Natural Language Processing
return '[data: %s]' %self.data (NLP): Sentiment Analysis II
(tokenization, stemming, and
>>> x = MyClass('secret')
stop words)
>>> print(x), x
[data: secret] [data: secret]
>>> myObjects = [MyClass('A'), MyClass('B')] Natural Language Processing
>>> for obj in myObjects: (NLP): Sentiment Analysis III
print(obj) (training & cross validation)

[data: A]
[data: B]
Natural Language Processing
(NLP): Sentiment Analysis IV
(out-of-core)
But not when we use print(myObjecs). Note that the instances are in the list:
Locality-Sensitive Hashing
(LSH) using Cosine Distance
>>> print(myObjects)
(Cosine Similarity)
[<__main__.MyClass instance at 0x02733800>, <__main__.MyClass instance at 0x027338C8>
>>> myObjects
[<__main__.MyClass instance at 0x027338C8>, <__main__.MyClass instance at 0x027336C0>

So, we need to de ne __repr__:


ARTIFICIAL NEURAL
NETWORKS (ANN)
>>> class MyClass:
def __init__(self, d):
self.data = d [Note] Sources are available
def __str__(self): at Github - Jupyter notebook
return '[str- data: %s]' %self.data les
def __repr__(self):
return '[repr- data: %s]' %self.data
1. Introduction
>>> myObjects = [MyClass('A'), MyClass('B')]
>>> for obj in myObjects: 2. Forward Propagation
print(obj)
3. Gradient Descent
[str- data: A]
[str- data: B]
>>> print(myObjects) 4. Backpropagation of Errors
[[repr- data: A], [repr- data: B]]
>>> myObjects 5. Checking gradient
[[repr- data: A], [repr- data: B]]
6. Training via BFGS
In his book, Learning Python, Mark Lutz summarizes as follows:
...__repr__, provides an as-code low-level display of an object when present. Sometimes classes 7. Over tting &
Regularization
provide both a __str__ for user-friendly displays and a __repr__ with extra details for
developers to view. Because printing runs __str__ and the interactive prompt echoes results
8. Deep Learning I : Image
with __repr__, this can provide both target audience with an appropriate display.
Recognition (Image
uploading)

9. Deep Learning II : Image


Recognition (Image
classi cation)

sys.argv 10 - Deep Learning III : Deep


Learning III : Theano,
sys.argv is the list of arguments passed to the Python program. TensorFlow, and Keras

The rst argument, sys.argv[0], is actually the name of the program. It exists so that we can
change our program's behavior depending on how it was invoked. For example:

1. sys.argv[0] is operating system dependent whether this is a full pathname or not.


2. If the command was executed using the -c command line option to the interpreter,
sys.argv[0] is set to the string '-c'.
3. If no script name was passed to the Python interpreter, sys.argv[0] is the empty string.

sys.argv[1] is thus the rst argument we actually pass to the program.

# t.py
if __name__ == '__main__':
import sys
print('program: %s' %sys.argv[0])
for arg in sys.argv[1:]:
print('Arg: %s' %arg)

If we run it with or without any argument, we get the output below:

C:\TEST>python t.py
program: t.py

C:\TEST>python t.py A B C
program: t.py
Arg: A
Arg: B
Arg: C

getopt.getopt(args, options[, long_options])


The getopt.getopt() parses command line options and parameter list. args is the argument
list to be parsed, without the leading reference to the running program. Typically, this means
sys.argv[1:]. options is the string of option letters that the script wants to recognize, with
options that require an argument followed by a colon (:).

long_options, if speci ed, must be a list of strings with the names of the long options which
should be supported. The leading -- characters should not be included in the option name.
Long options which require an argument should be followed by an equal sign (=). Optional
arguments are not supported. To accept only long options, options should be an empty string.
Long options on the command line can be recognized so long as they provide a pre x of the
option name that matches exactly one of the accepted options:
For example, if long_options is ['foo', 'frob'], the option --fo will match as --foo, but --f will not
match uniquely, so GetoptError will be raised.

The return value consists of two elements:

1. the rst is a list of (option, value) pairs


2. the second is the list of program arguments left after the option list was stripped (this is a
trailing slice of args).

Each option-and-value pair returned has the option as its rst element, pre xed with a hyphen
for short options (e.g., '-x') or two hyphens for long options (e.g., '--long-option'), and the
option argument as its second element, or an empty string if the option has no argument. The
options occur in the list in the same order in which they were found, thus allowing multiple
occurrences. Long and short options may be mixed.
-from http://docs.python.org/2/library/getopt.html.

# option.py

import getopt, sys

def main():

in_filename = 'fin'
out_filename = 'fout'

print('argv : %s' %(sys.argv[1:]))

try:
options, remainder = getopt.getopt(sys.argv[1:], 'i:o:', ['input=', 'output='
except getopt.GetoptError:
sys.exit()

print('options : %s' %(options))

for opt, arg in options:


if opt in ('-i', '--input'):
in_filename = arg
elif opt in ('-o', '--output'):
out_filename = arg

print('remainder : %s' %(remainder))

print('input file = %s' %(in_filename))


print('output file = %s' %(out_filename))

return 0

if __name__ == "__main__":
sys.exit(main())

If we run with two le long options:

$ python option.py --input=my_in_file --output=my_out_file r1 r2


argv : ['--input=my_in_file', '--output=my_out_file', 'r1', 'r2']
options : [('--input', 'my_in_file'), ('--output', 'my_out_file')]
remainder : ['remainer1', 'remainder2']
input file = my_in_file
output file = my_out_file

With short options:

$ python option.py -i my_in_file -o my_out_file


argv : ['-i', 'my_in_file', '-o', 'my_out_file']
options : [('-i', 'my_in_file'), ('-o', 'my_out_file')]
remainder : []
input file = my_in_file
output file = my_out_file

Note that the long option does not have to be fully matched:

$ python option.py --i=my_in_file --o=my_out_file


argv : ['--i=my_in_file', '--o=my_out_file']
options : [('--input', 'my_in_file'), ('--output', 'my_out_file')]
remainder : []
input file = my_in_file
output file = my_out_file

__call__
Python runs a __call__ method when an instance is called as a function form. This is more
useful when we do some interfacing work with APIs expecting functions. On top of that, we
can also retain state info as we see in the later examples of this section.

It is believed to be the third most commonly used operator overloading method, behind the
__init__ and the __str__ and __repr__ according to Mark Lutz (Learning Python).

Here is an example with a __call__ method in a class. The __call__ method simply prints out the
arguments it takes via keyword args.

>>> class Called:


def __call__(self, *args, **kwargs):
print("I've just called:", args, kwargs)

>>> A = Called()
>>> A('psy', 'Gangnam', 'Style')
I've just called: ('psy', 'Gangnam', 'Style') {}
>>> A (2013, 'Sept', 16, Singer='psy', Title='Gangnam Style')
I've just called: (2013, 'Sept', 16) {'Singer': 'psy', 'Title': 'Gangnam Style'}

As another example with a little bit more mixed arguments:

>>> class classA:


def __call__(self, *args, d=44, **kwargs):
print(args, d, kwargs)

>>> instanceA = classA()


>>> instanceA(1)
(1,) 44 {}
>>> instanceA(1,2)
(1, 2) 44 {}
>>> instanceA(1,2,3)
(1, 2, 3) 44 {}
>>> instanceA(1,2,3,4)
(1, 2, 3, 4) 44 {}
>>> instanceA(a=1, b=2, d=4)
() 4 {'a': 1, 'b': 2}
>>> instanceA(a=1, b=2, c=3, d=4)
() 4 {'a': 1, 'c': 3, 'b': 2}
>>> instanceA(*[1,2], **dict(c=333, d=444))
(1, 2) 444 {'c': 333}
>>> instanceA(1, *(2,), c=3, **dict(d=4))
(1, 2) 4 {'c': 3}

While the __call__ method allows us to use class instances to emulate functions as we saw in
the above examples, there is another use of __call__ method: we can retain state info:

>>> class State:


def __init__(self, state):
self.myState = state
def __call__(self, newInput):
return self.myState + newInput

>>> S = State(100)
>>> S(99)
199
>>> S(77)
177
>>>

CONTACT FOLLOW BOGOTOBOGO ABOUT US

BogoToBogo [email protected]
[email protected] 
Golden Gate Ave, San Francisco, CA 94115

Golden Gate Ave, San Francisco, CA 94115 Copyright © 2016, bogotobogo


Design: Web Master

You might also like