computational concepts toolbox
play

Computational Concepts Toolbox Data type: values, literals, Higher - PowerPoint PPT Presentation

Computational Concepts Toolbox Data type: values, literals, Higher Order Functions operations, Functions as Values Object Oriented Expressions, Call Functions with functions as argument expression Programming Assignment


  1. Computational Concepts Toolbox • Data type: values, literals, • Higher Order Functions operations, – Functions as Values Object Oriented • Expressions, Call – Functions with functions as argument expression Programming – Assignment of function • Variables values • Assignment Statement • Higher order function David E. Culler patterns • Sequences: tuple, list – Map, Filter, Reduce CS8 – Computational Structures in Data Science • Dictionaries • Function factories – create • Data structures http://inst.eecs.berkeley.edu/~cs88 and return functions • Tuple assignment • Recursion • Function Definition Lecture 8 – Linear, Tail, Tree Statement • Abstract Data Types March 28, 2016 • Conditional Statement • Mutation • Iteration: list comp, for, while • Lambda function expr. 2/22/16 2 UCB CS88 Sp16 L4 Today: class Review: Objects • Language support for object oriented • Objects represent information programming • Consist of data and behavior, bundled together • Defining a class introduces a new type of object to create abstractions – Abstract Data Types • It has attributes • They can have state • It has methods – mutable vs immutable • These implement its behaviors • Object-oriented programming – A methodology for organizing large programs – So important it is supported in the language (classes) • In Python, every value is an object – All objects have attributes – Manipulation happens through methods • Functions do one thing (well) – Object do a collection of related things 2/22/16 3 2/22/16 4 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  2. Administrative Issues Review: Bank account using dict account_number_seed = 1000 • Maps project part II due 3/30 def account(name, initial_deposit): • HW05 is lighter, but due 3/28 global account_number_seed account_number_seed += 1 return {'Name' : name, 'Number': account_number_seed, • Midterm “breakthrough” opportunity 'Balance' : initial_deposit} def account_name( acct ): – Thurs 9 - 1 return acct['Name'] def account_balance( acct ): return acct['Balance'] >>> my_acct = account('David Culler', 100) >>> my_acct def account_number( acct ): {'Name': 'David Culler', 'Balance': 100, return acct['Number'] 'Number': 1001} >>> account_number(my_acct) def deposit( acct , amount): 1001 acct['Balance'] += amount >>> your_acct = account("Fred Jones", 475) return acct['Balance'] >>> account_number(your_acct) 1002 def withdraw( acct , amount): >>> acct['Balance'] -= amount return acct['Balance'] 2/22/16 5 2/22/16 6 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 Python class statement Example: Account class BaseAccount: def init(self, name, initial_deposit): class ClassName: self.name = name new namespace <statement-1> self.balance = initial_deposit . def account_name(self): attributes . return self . name . <statement-N> The object def account_balance(self): return self.balance da dot def withdraw(self, amount): self.balance -= amount return self.balance methods 2/22/16 7 2/22/16 8 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  3. Creating an object, invoking a method Special Initialization Method class BaseAccount: The Class Constructor def __init__(self, name, initial_deposit): self.name = name my_acct = BaseAccount() self.balance = initial_deposit my_acct.init("David Culler", 93) my_acct.withdraw(42) def account_name(self): return self . name return None da dot def account_balance(self): return self.balance def withdraw(self, amount): self.balance -= amount return self.balance 2/22/16 9 2/22/16 10 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 Attributes and “private” Example • Attributes of an object accessible with ‘dot’ notation class BaseAccount: obj.attr def __init__(self, name, initial_deposit): • Alternative to selector/mutator methods self.name = name self.balance = initial_deposit • Most OO languages provide private instance fields def name(self): – Python leaves it to convention return self . name def balance(self): return self.balance def withdraw(self, amount): self.balance -= amount return self.balance 2/22/16 11 2/22/16 12 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  4. Example Example: “private” attributes class BaseAccount: class BaseAccount: def __init__(self, name, initial_deposit): def __init__(self, name, initial_deposit): self.name = name self._name = name self.balance = initial_deposit self._balance = initial_deposit def withdraw(self, amount): def name(self): self.balance -= amount return self ._ name return self.balance def balance(self): return self._balance def withdraw(self, amount): self._balance -= amount return self._balance 2/22/16 13 2/22/16 14 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 Class attributes Example: class attribute • Pertain to the class as a whole class BaseAccount: • Not to individual objects account_number_seed = 1000 • Name relative to class, not self def __init__(self, name, initial_deposit): self._name = name self._balance = initial_deposit self._acct_no = BaseAccount.account_number_seed BaseAccount.account_number_seed += 1 def name(self): return self ._ name def balance(self): return self._balance def withdraw(self, amount): self._balance -= amount return self._balance 2/22/16 15 2/22/16 16 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  5. More class attributes Inheritance • Define a class as a specialization of an existing class BaseAccount: account_number_seed = 1000 class accounts = [] • Inherent its attributes, methods (behaviors) def __init__(self, name, initial_deposit): self._name = name • Add additional ones self._balance = initial_deposit • Redefine (specialize) existing ones self._acct_no = BaseAccount.account_number_seed BaseAccount.account_number_seed += 1 – Ones in superclass still accessible in its namespace BaseAccount.accounts.append(self) def name(self): class ClassName ( inherits ): ... <statement-1> . def show_accounts(): . for account in BaseAccount.accounts: . print(account.name(), <statement-N> account.account_no(),account.balance()) 2/22/16 17 2/22/16 18 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 Example More special methods class Account(BaseAccount): def deposit(self, amount): self._balance += amount return self._balance class Account(BaseAccount): def deposit(self, amount): def __repr__(self): self._balance += amount return '< ' + str(self._acct_no) + return self._balance '[' + str(self._name) + '] >' def __str__(self): return 'Account: ' + str(self._acct_no) + '[' + str(self._name) + ']' def show_accounts(): for account in BaseAccount.accounts: print(account) 2/22/16 19 2/22/16 20 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  6. Classes using classes Key concepts to take forward • Class definition • Class namespace class Bank: accounts = [] • Methods def add_account(self, name, account_type, • Instance attributes (fields) initial_deposit): • Class attributes assert (account_type == 'savings') or (account_type == checking), "Bad Account type" • Inheritance assert initial_deposit > 0, "Bad deposit" • Superclass reference new_account = Account(name, account_type, initial_deposit) Bank.accounts.append(new_account) def show_accounts(self): for account in Bank.accounts: print(account) 2/22/16 21 2/22/16 22 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

Recommend


More recommend