Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modules and Objects Introduction to Computing Science and Programming I.

Similar presentations


Presentation on theme: "Modules and Objects Introduction to Computing Science and Programming I."— Presentation transcript:

1 Modules and Objects Introduction to Computing Science and Programming I

2 Modules We already know about several of Python’s built- in functions that are available for use at any point in any program. We already know about several of Python’s built- in functions that are available for use at any point in any program. int(), str(), round(), range(), etc. int(), str(), round(), range(), etc. Python provides a much larger library of useful functions. However, if Python made all of these available at all times speed would become an issue as Python would have to load the information on every function every time a program was run. Python provides a much larger library of useful functions. However, if Python made all of these available at all times speed would become an issue as Python would have to load the information on every function every time a program was run.

3 Modules These functions are organized into what are called modules that each contain a set of functions that are related. These functions are organized into what are called modules that each contain a set of functions that are related. For example there is a module called ‘math’ that contains a number of additional mathematical functions. For example there is a module called ‘math’ that contains a number of additional mathematical functions.

4 Importing Modules Before you can use the functions in a module you need to use an import statement that tells Python to read the module into memory. Before you can use the functions in a module you need to use an import statement that tells Python to read the module into memory. After importing the module, you access its functions by using the syntax modulename.function. After importing the module, you access its functions by using the syntax modulename.function.

5 Importing Modules Here’s a short program that uses the math module. Here’s a short program that uses the math module. import math angle = float(raw_input(“Enter an angle in radians: “)) print “The sine of the angle is“, print math.sin(angle) Note the import statement at the beginning of the program. For simplicity, you should place your import statements at the start of your programs. Note the import statement at the beginning of the program. For simplicity, you should place your import statements at the start of your programs.

6 Importing Modules If you only want to import a single function from with the module you can use this syntax If you only want to import a single function from with the module you can use this syntax from modulename import function from modulename import function This allows you to call the function without using the module’s name every time This allows you to call the function without using the module’s name every time from math import sin angle = float(raw_input(“Enter an angle in radians: “)) print “The sine of the angle is“, print sin(angle) You can also import all of the functions for use like this by using the * You can also import all of the functions for use like this by using the * from modulename import * from modulename import *

7 Documentation Online Documentation Online Documentation There is online documentation for all of Python’s modules. There is online documentation for all of Python’s modules. It isn’t always easy to understand, but you will become used to the descriptions with practice. It isn’t always easy to understand, but you will become used to the descriptions with practice.

8 Documentation The help function The help function You can access a lot of the documentation from within the Python interpreter. You can access a lot of the documentation from within the Python interpreter. Just type in help(name) where name is the name of a module or function that you want to see the documentation for. Just type in help(name) where name is the name of a module or function that you want to see the documentation for. If after Python has read a function you’ve defined and stored it, you can use the help function on it. Notice that the help you see is primarily the docstring you wrote for the function. If after Python has read a function you’ve defined and stored it, you can use the help function on it. Notice that the help you see is primarily the docstring you wrote for the function.

9 Objects So far while programming we’ve just been dealing with simple pieces of information such as strings, Booleans, or numbers. So far while programming we’ve just been dealing with simple pieces of information such as strings, Booleans, or numbers. Complex information and functions that can be used on them are available in objects. Complex information and functions that can be used on them are available in objects.

10 Objects We aren’t going to spend a great deal of time with objects in this course. However, if you continue on you will likely spend a lot of time with them. We aren’t going to spend a great deal of time with objects in this course. However, if you continue on you will likely spend a lot of time with them. Object-Oriented Programming is a powerful tool that languages such as Python, C++, and Java allow you to take advantage of. Object-Oriented Programming is a powerful tool that languages such as Python, C++, and Java allow you to take advantage of.

11 Objects There are different types of objects. Each type of object is known as a class. There are different types of objects. Each type of object is known as a class. Objects have properties that give you basic information on the object. Objects have properties that give you basic information on the object. Objects have methods that can be invoked to have the object perform some sort of task. Objects have methods that can be invoked to have the object perform some sort of task.

12 Objects A real-world analogy. A real-world analogy. A DVD Player as an object A DVD Player as an object Class – DVD Player Class – DVD Player Properties – Information displayed on the DVD player such as the time within the movie being played Properties – Information displayed on the DVD player such as the time within the movie being played You can change the values of properties just like variables You can change the values of properties just like variables Methods – The buttons on the DVD player give you access to methods such as Play, Pause, etc. Methods – The buttons on the DVD player give you access to methods such as Play, Pause, etc. Each occurrence of a class is known as an instance of that class Each occurrence of a class is known as an instance of that class

13 Objects Objects in Python Objects in Python Classes are defined in many of Python’s modules and can be written by the programmer. (though we won’t be in this course) Classes are defined in many of Python’s modules and can be written by the programmer. (though we won’t be in this course) When you need an object of a certain type, you instantiate it (create an instance of it) When you need an object of a certain type, you instantiate it (create an instance of it) To instantiate an object, you call the constructor, a special method that each class defines To instantiate an object, you call the constructor, a special method that each class defines

14 Objects The datetime module contains a class called date that holds information about a particular day The datetime module contains a class called date that holds information about a particular day The constructor for a class has the same name as the class and objects are stored with variables, just like simple data types. The constructor for a class has the same name as the class and objects are stored with variables, just like simple data types.

15 Objects To create a date object you use code like this To create a date object you use code like this import datetime today = datetime.date(2007,6,11) today.strftime(“%B %d, %Y”) As with accessing functions in modules, you use the dot operator to access methods from an instance of a class As with accessing functions in modules, you use the dot operator to access methods from an instance of a class Like the time module which we looked at in class and is mentioned in the study guide, datetime has strftime() available Like the time module which we looked at in class and is mentioned in the study guide, datetime has strftime() available

16 Objects The date object has the properties year, month, and day which can also be accessed using the dot operator The date object has the properties year, month, and day which can also be accessed using the dot operatortoday.daytoday.year Depending on how a class was written you may be able to assign values to properties as with variables, but this is not true of the date class Depending on how a class was written you may be able to assign values to properties as with variables, but this is not true of the date class

17 Objects Classes may define how to be used with mathematical operators. Classes may define how to be used with mathematical operators. date cannot be added, but can be subtracted date cannot be added, but can be subtracted import datetime today = datetime.date(2007,6,11) yesterday = datetime.date(2007,6,10) print today-yesterday #works print today+yesterday #causes an error

18 Objects The date class also defines how it can be converted into a string. This allows it to be used with the print command. Not all classes will have this defined. The date class also defines how it can be converted into a string. This allows it to be used with the print command. Not all classes will have this defined.


Download ppt "Modules and Objects Introduction to Computing Science and Programming I."

Similar presentations


Ads by Google