PYTHON Reviewer
PYTHON Reviewer
Compiled Language
State
▪ characteristic of object
▪ fields, properties.
Behavior Constructor Method
• can be called methods, class
• special method that is used to
functions.
initialize a newly created object and
• actions you can do w/ objects.
is called just after the memory is
allocated for the object.
Declaration • If no user-defined constructor is
provided for a class, compiler
• statement that describes an
initializes member variables to its
identifier, such as the name of a
default values.
variable or a function.
• Arithmetic: +,-/,*,%,//,**
Looping • Relational: <,<=, >, >=, not=, ==
• Logical: not, and, or
Python has two primitive loop commands:
(Pointers to review from other section) • use the pass keyword when you do
not want to add any other properties
INHERITANCE or methods to the class.
• eg:(full program)
Parent Class
class Person:
class Person:
def __init__(self, fname, lname): def __init__(self, fname, lname):
self.firstname = fname self.firstname = fname
self.lastname = lname self.lastname = lname
• eg: # Constructor
def __init__(self, name, age):
class Base1(object): Base.__init__(self, name)
def __init__(self): self.age = age
self.str1 = "Geek1"
print "Base1" # To get name
def getAge(self):
class Base2(object): return self.age
def __init__(self):
self.str2 = "Geek2" # Inherited or Sub class (Note
print "Base2" Person in bracket)
class GrandChild(Child):
class Derived(Base1, Base2):
def __init__(self): # Constructor
def __init__(self, name, age,
# Calling constructors of address):
Base1 Child.__init__(self, name,
# and Base2 classes age)
Base1.__init__(self) self.address = address
Base2.__init__(self) # To get address
print "Derived" def getAddress(self):
return self.address
def printStrs(self):
print(self.str1, self.str2) # Driver code
g = GrandChild("Geek1", 23,
"Noida")
print(g.getName(), g.getAge(),
ob = Derived() g.getAddress())
ob.printStrs()
ENCAPSULATION • A double underscore: Private
• It is the restriction of access to variable, harder to access but
methods and variables. This can still possible.
prevent the data from being
modified by accident. Setters and Getters
• uses private and public
• similar to Java's concept of
variables/methods. But does not
encapsulation and uses setters
have private/public keyword like in
and getters.
Java
• Private variables are intended to be
• Other programming languages
changed using getter and setter
have protected class methods too,
methods. These provide indirect
but Python does not. access to them.
• eg: • eg:
• class Robot(object):
• class Robot(object):
def __init__(self):
def __init__(self):
self.a = 123
self.__version = 22
self._b = 123
self.__c = 123
def getVersion(self):
print(self.__version)
obj = Robot()
print(obj.a)
def setVersion(self,
print(obj._b)
version):
print(obj.__c)
self.__version =
version
• if you print the results, you will see
that . obj.__c will not be printed
because it is a private variable. obj = Robot()
• A variable is private if it has double obj.getVersion()
underscores before it. obj.setVersion(23)
class Bear(object):
Output: 15
def sound(self):
print("Groarrr")
• A lambda function that multiplies
class Dog(object):
def sound(self):
argument a with argument b and
print("Woof woof!") print the result:
makeSound(bearObj)
makeSound(dogObj)
Output:
Groarrr
Woof woof!