Cheat Sheet Python 5.docx - 15 Classes and Objects u2013...

This preview shows page 1 - 2 out of 2 pages.

15. Classes and Objects – Basics (Object-oriented) 15.2 User – defined compound data types objects : contain both data & functionality. class Point : """ docstring explanation below class """ def __init__ ( self ): # Constructor method """ another explanation here """ self . x = 0 # acts as variables self . y = 0 # another variable ------------------------------------------------ p = Point() # Instantiate obj. of type Point q = Point() # Make a second point ------------------------------------------------ print (p . x, p . y, q . x, q . y) # Each point object has its own x and y ------------------------------------------------ 0 0 0 0 # output - methods : functions ( def ) within a class - Initializer method __ init __ : automatically called whenever a new instance of Point is created. Set up attributes required w/ new instance by giving them their initial state/values. Name of the constructor method within class definition. - Self : [we could choose other name, but self is the convention] automatically set to reference newly created object that needs to be initialized. IN THE FIRST PARAMETER. - Constructor : A function, like Point class, creates a new object instance called constructor . Every class automatically provides a constructor function, the same name as the class. - Instantiation : the combined process of “make me a new object” and “get its setting initialized to the factory [class] default setting”. [kind of like initializing a variable w/ a type] - instance : an object (variables) whose type is of some class. Instance and object are used interchangeably. [aka attributes] 15.3 Attributes - object instances have both attributes and methods. >>> p . x = 3 >>> p . y = 4 -attributes : the syntax for accessing names -Variable p refers to a Point object, which contains two attributes. Each attribute refers to a number. >>> print (p . y)  4 # access value of an attr. >>> x = p . x >>> print (x)  3 p.x means, “Go to object p refers to and get the value of x ”. print ( "(x={0}, y={1})" . format(p . x, p . y)) distance_squared_from_origin = p . x * p . x + p . y * p . y Output : (x=3, y=4)  25 15.4 Improving our initializer class Point : """ Point class manipulates x,y coords. """ def __init__ ( self , x = 0 , y = 0 ): """ Create a new point at x, y """ self . x = x self . y = y # Other statements outside class continue below: ------------------------------------------------ >>> p = Point( 4 , 2 ) >>> q = Point( 6 , 3 ) >>> r = Point() # r represents the origin (0, 0) >>> print (p . x, q . y, r . x) 4 3 0 15.5 Adding other methods to our class A method [access w/ (.)] behaves like a function but it is invoked on a specific instance, e.g. tess.right(90) . class Point : """ Point class manipulates x,y coords. """ def __init__ ( self , x = 0 , y = 0 ): """ Create a new point at x, y """ self . x = x self . y = y # adding another method to our class: def distance_from_origin ( self ): """ Compute distance from the origin """ return (( self . x ** 2 ) + ( self . y ** 2 )) ** 0.5 ------------------------------------------------ >>> p = Point( 3 , 4 ) >>> p . x  3 >>> p . y  4 >>> p . distance_from_origin()  5.0 # new method >>> q = Point( 5 , 12 ) >>> q . x  5 >>> q . y  12 >>> q . distance_from_origin()  13.0 >>> r = Point() >>> r . x  0 >>> r . y  0 >>> r . distance_from_origin()  0.0 15.6 Instances as arguments and parameters def print_point (pt): # pt used as ‘self’ print ( "({0}, {1})" . format( pt . x , pt . y )) print_point(p)  (3,4) # object p from before print_point takes a point as an argument and formats the output in whichever way we choose.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture