What Is in a Name Modules and Classes - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

What Is in a Name Modules and Classes

Description:

sys.path.append(myModulePath) We have been using modules since the beginning. Modules ... sys.path.append(myModulePath) Why Would I Want to Use a Module? ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 16
Provided by: Ada581
Category:
Tags: append | classes | modules | name

less

Transcript and Presenter's Notes

Title: What Is in a Name Modules and Classes


1
What Is in a Name?(Modules and Classes)
2
Modules
  • Modules create a separate name space for
    functions and classes to be declared it.
  • For instance,
  • import sys
  • sys.path.append(myModulePath)
  • We have been using modules since the beginning

3
Modules
  • Modules are just python files
  • E.g. mymodule.py
  • Modules are used with the import statement
  • import mymodule
  • Of course the interpreter needs to know where to
    find the specified

4
Oh, Module! Where are thou?
  • Module location is system dependent information
  • In the module sys there is a list called path
  • This is the module search path
  • It is a list of strings where each string is a
    directory path
  • sys.path.append(myModulePath)

5
Why Would I Want to Use a Module?
  • To group together related functions and variables
  • All programs are modules
  • To debug projects with the interactive debugger

6
Modules are Nice, But Do They Have Class?
  • Someone may have noticed that in C classes
    allow you to group together functions and
    variable
  • However, modules are not classes
  • Python has class, too
  • But they are a little bit different

7
ClassesWhat are They Good For?
  • Creating new types
  • What is a type?
  • A set of variables (Class Variables) grouped with
    a set of function (Class Methods)
  • Variable can be assigned type instances (e.g.
    class instances)
  • You can think of a class as a module that you can
    instantiate a class instance and assign that
    instance to a variable

8
But What Does That Mean?
  • It means that we can declare a class like Account
    to represent a bank account
  • Lets say the each Account class has a function
    withdraw(Amount)
  • Now we can use Accounts like any other type
  • e Account("Bill", 50.0)
  • e.withdraw(45.0)

9
But How Do We Declare a Class?
  • It all starts with a declaraction
  • class Account(object)
  • A simple bank account record
  • def __init__ (self, name, balance)
  • Account Constructor
  • self.__name name Create a private
    class variable name
  • self.__balance float(balance) Create
    a private balance
  • return

10
How Do I Make Some Real Methods?
  • class Account(object)
  • A simple bank account record
  • def deposit(self, Amount)
  • deposit Amount into self
  • self.__balance Amount
  • return
  • def withdraw(self, Amount)
  • withdraws Amount from self
  • self.__balance - Amount
  • return

11
What was all that?
  • Class starts the class declaration
  • For this class consider the use of object
    mandatory magic
  • Functions declared in the class block are methods
    in the class
  • __init__() is the name of the class constructor
  • __init__() can have any number of parameters
  • Names inside a class block that are prefixed __
    are private names

12
With Class Methods the First Parameter is Special
  • Python does not have keyword like this or me to
    designate the calling class instance
  • Calls to class methods pass the calling instance
    as the first parameter to the method function
  • e.toString()
  • Account.toString(e)
  • Convention is to call the first parameter self

13
How Do I Make Some Real Methods?
  • class Account(object)
  • A simple bank account record
  • def transfer(self, ToAccount, Amount)
  • Transfers Amount from self to ToAccount
  • self.withdraw(Amount)
  • ToAccount.deposit(Amount)
  • return
  • def balance(self)
  • return self.__balance
  • def name(self)
  • return self.__name

14
How Do I Make Some Real Methods?
  • class Account(object)
  • A simple bank account record
  • def toString(self)
  • return self.__name " 4.2f"
    self.__balance

15
Class Challenge
  • Make a class called Bank that defines the
    following methods
  • withdraw(Who, Amount)
  • deposit(Who, Amount)
  • transfer(Who, Amount)
  • balance(Who)
  • add(Who, Amount)
  • toString()
Write a Comment
User Comments (0)
About PowerShow.com