Procedural (C) Vs OOP (C++/Java/Python) : (Sys Version)
Procedural (C) Vs OOP (C++/Java/Python) : (Sys Version)
In [139]:
import sys
print (sys.version)
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 1/15
07/04/2020 LIVE_OOP_Basics_Python
In [150]:
import random
import string
d = dict();
r = "https://www.shortURL.com/"+shortURL
return r;
def getLongURL(shortURL):
if k in d:
return d[k];
else:
return None;
Class: datatype
Object: variable
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 2/15
07/04/2020 LIVE_OOP_Basics_Python
In [50]:
class ShortURL:
r = "https://www.shortURL.com/"+shortURL
return r;
if k in self.d:
return self.d[k];
else:
return None;
In [51]:
print(type(s))
<class '__main__.ShortURL'>
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 3/15
07/04/2020 LIVE_OOP_Basics_Python
The script invoked directly is considered to be in the main module. It can be imported and accessed the
same way as any other module.
Modules:
[https://www.w3schools.com/python/python_modules.asp
(https://www.w3schools.com/python/python_modules.asp)]
Consider a module to be the same as a code library. A file containing a set of functions you want to include
in your application.
In [52]:
print(s.shortURL("appliedaicourse"))
print(s.shortURL("gate.appliedcourse.com"))
--------------------------------------------------------------------
-------
AttributeError Traceback (most recent cal
l last)
<ipython-input-52-84023b7d1bbb> in <module>
----> 1 print(s.shortURL("appliedaicourse"))
2 print(s.shortURL("gate.appliedcourse.com"))
In [53]:
print(s.getShortURL("appliedaicourse.com"))
print(s.getShortURL("gate.appliedcourse.com"))
https://www.shortURL.com/alrpoy
https://www.shortURL.com/dlfjxuxhff
In [54]:
print(s.getLongURL("https://www.shortURL.com/alrpoy"))
appliedaicourse.com
In [55]:
print(s.d)
In [59]:
s.d["interviewprep.appliedcourse.com"] = "abcdefgh";
print(s.d)
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 4/15
07/04/2020 LIVE_OOP_Basics_Python
In [151]:
e = EmptyClass();
print(type(e))
<class '__main__.EmptyClass'>
There are lots of internals and boundary cases for which reading
documentation is a good idea.
https://docs.python.org/3/tutorial/classes.html (https://docs.python.org/3/tutorial/classes.html)
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 5/15
07/04/2020 LIVE_OOP_Basics_Python
In [77]:
class ShortURL1:
r = self.URLPrefix + shortURL
return r;
if k in self.d:
return self.d[k];
else:
return None;
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 6/15
07/04/2020 LIVE_OOP_Basics_Python
In [78]:
s1 = ShortURL1();
print(s1.getShortURL("appliedaicourse.com"))
print(s1.getShortURL("gate.appliedaicourse.com"))
https://www.shortURL.com/bqrkdpum
https://www.shortURL.com/jaxgluaeoh
In [79]:
print(s1.d)
print(s1.URLPrefix)
In [81]:
s1a = ShortURL1();
print(s1a.URLPrefix);
print(s1a.d)
https://www.shortURL.com/
{}
If you have learnt OOP in C++/Java, it is very easy to get confused with syntax and internals. Please
beware
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 7/15
07/04/2020 LIVE_OOP_Basics_Python
In [88]:
# "Private Members"
class ShortURL2:
classVar = "test"; # Public => accesible directly from outside the class
r = self.__URLPrefix + shortURL
return r;
if k in self.d:
return self.d[k];
else:
return None;
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 8/15
07/04/2020 LIVE_OOP_Basics_Python
In [90]:
s2 = ShortURL2();
print(s2.classVar)
print(s2.__URLPrefix)
test
--------------------------------------------------------------------
-------
AttributeError Traceback (most recent cal
l last)
<ipython-input-90-4fd735b2fbbe> in <module>
1 s2 = ShortURL2();
2 print(s2.classVar)
----> 3 print(s2.__URLPrefix)
Abstraction:
[https://en.wikipedia.org/wiki/Abstraction_principle_(computer_programming
(https://en.wikipedia.org/wiki/Abstraction_principle_(computer_programming))]
As a recommendation to the programmer, in its formulation by Benjamin C. Pierce in Types and
Programming Languages (2002), the abstraction principle reads: “ Each significant piece of functionality
in a program should be implemented in just one place in the source code. Where similar functions are
carried out by distinct pieces of code, it is generally beneficial to combine them into one by abstracting
out the varying parts. ”
General concept in porgramming: libraries, classes
"Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user
to implement more complex logic on top of the provided abstraction without understanding or even
thinking about all the hidden complexity." [Source: https://stackify.com/oop-concept-abstraction/
(https://stackify.com/oop-concept-abstraction/)]
Encapsulation:
[https://en.wikipedia.org/wiki/Encapsulation_(computer_programming
(https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))]
In object-oriented programming languages, and other related fields, encapsulation refers to one of two
related but distinct notions, and sometimes to the combination thereof:
1. A language mechanism for restricting direct access to some of the object's components.
2. A language construct that facilitates the bundling of data with the methods (or other functions) operating
on that data.
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 9/15
07/04/2020 LIVE_OOP_Basics_Python
In [105]:
class ShortURLFinal:
r = self.URLPrefix + shortURL
return r;
if k in self.d:
return self.d[k];
else:
return None;
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 10/15
07/04/2020 LIVE_OOP_Basics_Python
In [109]:
r = self.URLPrefix + shortURL
return r;
In [110]:
print(m1.d)
{}
In [111]:
print(m1.getShortURL("amazon.com"))
print(m1.getShortURL("google.com"))
www.myurlshortner.com/mt2e9h
www.myurlshortner.com/ymnsyufq3
In [112]:
print(m1.d)
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 11/15
07/04/2020 LIVE_OOP_Basics_Python
In [157]:
class A:
def __init__(self, i):
self.var = i;
def printVar(self):
print(self.var)
a = A(10);
a.printVar()
a = A(20);
a.printVar()
10
20
In [158]:
a =10;
print(type(a))
<class 'int'>
In [159]:
a =20;
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 12/15
07/04/2020 LIVE_OOP_Basics_Python
In [115]:
class Shape:
def get_color(self):
return self.__color
def get_filled(self):
return self.__filled
class Rectangle(Shape):
def get_length(self):
return self.__length
def get_breadth(self):
return self.__breadth
def get_area(self):
return self.__length * self.__breadth
def get_perimeter(self):
return 2 * (self.__length + self.__breadth)
class Circle(Shape):
def __init__(self, radius):
super().__init__()
self.__radius = radius
def get_radius(self):
return self.__radius
def get_area(self):
return math.pi * self.__radius ** 2
def get_perimeter(self):
return 2 * math.pi * self.__radius
r1 = Rectangle(10.5, 2.5)
c1 = Circle(12)
In [ ]:
In [ ]:
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 14/15
07/04/2020 LIVE_OOP_Basics_Python
In [161]:
a = [1,2,3,4];
print(type(a))
print(a)
<class 'list'>
[1, 2, 3, 4]
In [116]:
c1 = ClassWithStr();
print(c1)
Example: https://matplotlib.org/3.1.1/api/axis_api.html
(https://matplotlib.org/3.1.1/api/axis_api.html)
In [ ]:
localhost:8888/nbconvert/html/CodeWalkthroughSessions/LIVE_OOP_Basics_Python.ipynb?download=false 15/15