Class Notes (835,573)
Canada (509,252)
CSC148H1 (92)
Paul Gries (18)
Lecture

jan16c.docx

3 Pages
123 Views
Unlock Document

Department
Computer Science
Course
CSC148H1
Professor
Paul Gries
Semester
Winter

Description
 Objects o Objects are modular entities that have both state (data) and behaviour (methods) bundled together, much like verbs and nouns work together. o Object-oriented programming uses objects and their interactions as the building blocks for programs. o It lets programmers build networks of cooperating objects, either to model real-world entities, or to fulfill particular roles as components in software systems. o In Python, everything is an object.  Even the simplest data structures (like ints) come with all kinds of built-in behaviours.  Classes o All objects of a particular type share common behavior and traits  For ex. all the lists are indexable o A class is a type in Python, and describes an object’s state and behaviour. o We define a class with the following syntax: class Classname(object): ‘’‘Docstring goes here’’’ # methods go here  Style reminder o Class names: CamelCase capitalized o Method names: pothole_case  all lowercase, words separated with underscores o Variable names: pothole_case  all lowercase, words separated with underscores o Constant names: ALL_CAPS_WITH_UNDERSCORES  all uppercase, words separated with underscores  these are constant by convention: you can still assign to them, but this will confuse other programmers o Instance variables, “private” methods: _beginning_underscore  this is another convention  http://www.python.org/dev/peps/pep-0008/ o Key sections: Code lay-out, Whitespace in Expressions and Statements, Naming Conventions  Initializers o As we have seen, classes are abstract definitions/blueprints/templates  Ex. Person o In contrast, objects are concrete entities in memory with addresses and data in them  Ex. me. o How do we initialize objects of a particular class? o Every class has a constructor: a method that takes in the values of the object's attributes (the data) and makes an object of that class with those values. o In Python, the constructor has the special name __init__  Ex. The Point Class o Let's say we want to create a class called Point that corresponds to a 2-dimensional point in a Cartesian coordinate plane. o We start with a class definition and a constructor: class Point(object): ‘’’An (x, y) coordinate.’’’ def __init__(self, ax, ay): ‘’‘(Point, int, int) -> Point A 2D Point, (ax, ay).’’’ self.x = ax self.y = ay  Self o self is the convention for naming the memory address of a particular object. o When a new object is created, like so: >>> x = Point(3, 5) o Python does the following:  allocates a chunk of memory for a new Point object  calls the Point class's __init__ method, passing it the memory address of the object as the first parameter and the memory address of int objects 3 and 5 as the next two  returns the memory address of the new object  Object Methods 1. class Point(object): 2. def __init__(self, ax, ay): 3. self.x = ax 4. self.y = ay 5. 6. def move_x(self, dist): 7. ‘’‘(Point, int) -> NoneType 8. Add dist to the x coordinate.’’’ 9. self.x += dist 10. 11. def move_y(self, dist): 12. ‘’‘(Point, int) -> NoneType 13. Add dist to the x coordinate.’’’ 14. self.y += dist o How does the hidden argument self work with method invocations? o For example: apoint.move_x(5) o translates to: Point.move_x(apoint, 5) o which is how self gets a value in the definition of move_x.  Encapsulation
More Less

Related notes for CSC148H1

Log In


OR

Join OneClass

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

Sign up

Join to view


OR

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.


Submit