Upload
others
View
17
Download
0
Embed Size (px)
Citation preview
Lecture 7-8: Python ●Lectures 1-6: introduction to Linux
● Using the command line● Bash scripting● Conditional statements● Loops● Input and output
●Lectures 7-8 Getting going with python in Linux● Using python in Linux● Object orientated programming
Python
● Python is and interpreted language– The script (text document) is interpreted line by line as it is run
InterpreterText file Machine code
Python interpreters are available for Windows,Linux, OSX,android....
#!/usr/bin/env python
Adding the line “#!/usr/bin/env python”Identifies this as a python script
This script can then be run the same as a bash script
Python interpreter in LinuxScript (text document)
Running the script with python
Stdin, stdout and stderr can be used the same as a bash script
Computing paradigms(The basic structure of thought underlying the programming activity)
Characteristics of OOP
● Discipline and idea– The theory of concepts, and models of human interaction with real world phenomena
● Data as well as operations are encapsulated in objects● Information hiding is used to protect internal properties of an object● Objects interact by means of message passing
– A metaphor for applying an operation on an object
● In most object-oriented languages objects are grouped in classes– Objects in classes are similar enough to allow programming of the classes, as
opposed to programming of the individual objects
– Classes represent concepts whereas objects represent phenomena
● Classes are organized in inheritance hierarchies– Provides for class extension or specialization
What is an object?
What is an object?
An object is a member of a class, so what is a class?
An object is a member of a class, so what is a class?
Class variable
Class Name
Documentation string
The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.
You declare other class methods like normal functions with the exception that the first argument to each method is self. Python adds the self argument to the list for you; you don't need to include it when you call the methods.
Creating instances of objects
To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.
Accessing object properties using the ”.” operator
Alternative method for accessing object attributes
Built in class attributes
__dict__ : Dictionary containing the class's namespace.
__doc__ : Class documentation string or None if undefined.
__name__: Class name.
__module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode.
__bases__ : Contains the base classes, in the order of their occurrence in the base class list.
Built in attributes
Inheritance
Derived class
You can create a class by deriving it from a pre existing class
The child class inherits the attributes of its parent class
Inheritance example
Output from the code:
Exercise 1 PolygonsThis script will draw a triangle using the module turtle
Write a class that store the properties of a regular polygon (size,angle,sides). The class should contain a function that will draw the object
Exercise 2 Koch snowflake
This Koch snowflake was discovered in 1904 by Swedish mathematician Helge von Koch
1)divide the line segment into three segments of equal length.2)draw an equilateral triangle that has the middle segment from step 1 as its base and points
outward.3)remove the line segment that is the base of the triangle from step 2.
Fig 1
Fig 2
Exercise 2 continued
a) Write a function using turtle that draws the path A-B-C-D-E from Figure 2.
b) Write a function that draws the second order Kosh curve by calling the function written in 2a.
c) Write a function that draws a Kosh curve to an arbitrary nth order by calling itself.
d) Write a class that store the properties of a Koch snowflake (size,angle,sides). The class should contain a function that will draw the object