20 - further programming (pyhton files) Flashcards
programming paradigm
set of programming concepts
eg low level, imperative, object oriented, declarative
OOP
programming methodology that uses self contained objects containing programing statments and data that communicate with each other
class
defines the methods and data of a certain type of object
attributes
data items in a class
method
a programmed procedure which is defined as part of a class
encapsulation
process of putting data and methods together as a single unit, a class
object
an instance of a class that is self-contained and includes data and methods
property
data and methods within an object that perform a named action
instance
An occurrence of an object during the execution of a program
data hiding
technique which protects the integrity of an object by restricting access to the
data and methods within that object
__ makes attributes private in python so need getters to retrieve attributes
inheritance
process in which the methods and data from one class, a superclass or base class, are copied to another class - a derived class
polymorphism
feature of object-oriented programming that allows methods to be redefined for derived classes
overloading
feature of object-oriented programming that allows a method to be defined more than once in a class, so it can be used in different situations
containment
a relationship in which one class has a component that is of amnother class type
one class contained in another type
getter
a method that gets the value of a property
setter
a method used to control changes to a variable
constructor
a method used to initialise a new object
destructor
a method that is automatically invoked when an object is destroyed
declarative programming
statements of facts (a thing thats known) and rules (relationships between facts) together with a mechanism for setting goals in the form of a query
defining a class in python
class person ():
def __init__ (self, name, age)
self.__name = name
self.__age = age
def GetName(self):
return self.__name
def GetAge(self):
return self.__age
setting up an instance of an object
thisperson = person(“bob”, 20)
inheritance - derived class python
class employee(person):
def __innit__(self, name, age):
person.__init__(self, name, age)
self.__hours = 0
def GetHours(self)
return self.__hours
polymorphisim in python
python overloading
class greeting():
def hello(self, name = None):
if name is not None
print(“Hello” +name)
else:
print(“Hello”)
thisgreeting = greeting()
thisgreeting.hello()
(when no parameters it just prints hello)
thisgreeting.hello(“bob”)
(when there is a parameter it prints hello name)
setter python
def SetName(self, name):
self.__name = name
getter python
def GetName(self):
return self.__name
destructor python
def __del__(self):
print(“deleted”)
exception handling
responding to an exception within the program so that the program doesnt stop unexpectedly
storing records in a sequential file
opening a file pyhton
f = open(‘filename’ , ‘r’)
‘r’ is read
‘a’ is append
‘w’ is write
exception handling in python
try:
<statement>
except <error>:
print('error')
</error></statement>
+ OOP
+ produces robust, more reliable code - attributes can only be manipulated using methods - protected from accidental change
+ classes provided in module libraries are thoroughly tested
serial files
contains records that have not been organised in an order
sequential files
has ordered records
imperative programming paradigm
writing a program as a sequence of explicit steps that are executed by the processor
object oriented programming paradigm
based on objects interacting with one another objects are data structures with associated methods
declarative programming paradigm
writes down a specification that describes what th eproblem is not what the computer should do - sets up queries for the programming system to decide what to do with eg SQL
low level programming paradigm
gives us the ability to manipulate contents of meory and registers directly and exploit the architecture of a given processor
random files
a file that stores records at specific addresses that can be accessed directly (direct access storage)