I&C SCI 33 Lecture 7: [Class Review]

5 Pages
Unlock Document

University of California - Irvine
Information and Computer Science
I&C SCI 33
Richard Pattis

Lecture 7 [Class Review]  Thursday, April 20, 2017  5:16 PM  Defining Classes  • When we define a class in a Python, we are binding the class name to an object representing the entire class.  • That class object contains mostly methods, but it can also contain data that is common to all the objects constructed from the class.  • Defining  class C:  …  Creates the name C and binds it to an object representing class C.    • When we want to construct an object that is an instance of a class, we refer to  a class object (typically by a name bound to it) followed by ()  ○ Then 3 things happen:  i. It calls a special function that creates an object that is a new instance of the class.   • Note that this object automatically has an dictionary associated with it, with the name __dict__, and that dictionary starts empty.    ii. It calls the __init__ method for the class, passing the object created in (i) to the first/self parameter of __init__ , and following this with all the other argument values in the parentheses used in the call to construct the state of this instance.   • Typically it assigns values to self/instance variables, which stores the name/binding in __dict__ for the self object.    iii. A reference to the object that was created in (i) and initialized in (ii) is returned as the result of constructing the object:   • Most likely this reference will be bound to a name: e.g., c = C(...) which means c refers to a newly constructed/initialized object of class C (again, the new object is an instance of class C).    Manipulating an object's namespace (and __init__)  • All objects have namespaces, which are dictionaries in which attribute names defined in that object are bound-to/associated-with values.   ○ self.name = value inside __init__ function  ○ This creates an entry in the __dict__ that looks like: {name: value}  • Alternate (not recommended) ways:  c = C()  c.x = 1  c.__dict__['z'] = 3    print(c.__dict__) --> prints {'x':1, 'z': 3}    Interlude: assert in initialization  ○ The form of assert is:    assert [boolean­test], [string]    ○ The string to AssertionError should always contain 4 parts:  i. The name of the class/module  ii. The name of the method/function that causes the problem  iii. A short desc of the problem  iv. The values of the argument(s) that are causing the problem  ○ Example for __init__    def __init__(self,x):  assert type(x) is int and x > 0, 'C.__init__: x('+str(x)+') must be an int and positive'    ○ This way, we can place limits on the parameters the user enters    • Once an object is constructed/initialized, we use it by calling methods on it  • *Remember:  o.m(...) is translated into type(o).m(o,...)  • Note about if __name__ == '__main__'  ○ If a module were imported, the __module__ key in the object dictionary would be associated with the file/module name it was written in.  ○ The __name__ variable in a module is bound to '__main__' in a started script, but it is bound to the module's name in any module that is imported into another module.    • if module m1.py contained:  print('in m1',__name__)   if __name__ == '__main__':  print('m1 is running')    --> Running m1.py produces  in m1 __main__  m1 is running    • If in addition module m2.py contained  import m1   print('in m2',__name__)   if __name__ == '__main__':       print('m2 is running')    -->Running m2.py produces  in m1 m1   in m2 __main__  m2 is running    Different kinds of names/attributes: definition and use  • Let's discuss four different kinds of names in relation to classes. We will call all these names variables.  a. local variables: defined and used inside functions/methods to help with the computation; parameter variables are considered local variables too. These are created during the execution of the function/method; they disappear when the function/method terminates.    b. instance variables of an object: typically defined inside __init__ and used inside class methods (we saw other ways to define them above too). These are referred to as self.name. These exist so long as the object exists.    c. class variables: typically defined in classes (at same level as methods, not inside methods) and typically used in methods; we use a class variable for information COMMON to all objects of a class (rather than putting the same instance variable in each object constructed from the class). Methods are actually class variables, bound to function objects.    d. global variables: typically defined in modules (outside of functions and classes) and used inside functions and/or class methods; we typically avoid using them (don't use global variables), and when we do use them, we do so in a cautious and limited manner.  • Sample class:  global_var = 0    class C:  class_var = 0    def __init__(self,init_instance_var):           self.instance_var = init_instance_var    def bump(self,name):           print(name,'bumped')           #global_var = 100  
More Less

Related notes for I&C SCI 33

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.