CS2304: Object Oriented Programming - Virginia...
Transcript of CS2304: Object Oriented Programming - Virginia...
CS2304: Python for Java Programmers
Monti 2014
CS2304: Object Oriented Programming
CS2304: Python for Java Programmers
Monti 2014
Object Oriented Python
• Python is a multi-paradigm language so you can program in several different styles.
• We’ve focused procedural code so far, but objects are everywhere.
• Strings, numbers, sequences are all objects in Python.
CS2304: Python for Java Programmers
Monti 2014
General Form
• Note the self everywhere. It’s required and similar to this.
• Remember indentation is very important.
class ClassName(Parent1, Parent2): # constructor def __init__(self): self.member1 = ...
# member methods def classFunction(self, var1): statements1
CS2304: Python for Java Programmers
Monti 2014
A Relevant Example
• My CurrentFile class from Project 2, note the indentation:
class CurrentFile: # constructor, initialize the state of file def __init__(self): self.is_open = False self.is_saved = True self.data = "" self.name = "" self.cursor = 0 ...
CS2304: Python for Java Programmers
Monti 2014
A Relevant Example: Part II • __str__ is like toString() in Java and I use it
print the file: def __str__(self): rv = ””; pos = 0 lines = self.data.split("\n") for line in lines: length = len(line) if length != 0: if (self.data[len(self.data) - 1] != "\n" and x == len(lines) - 1): length -= 1 # format from the project spec rv += “...".format(line, pos, pos + length)
CS2304: Python for Java Programmers
Monti 2014
A Relevant Example: Part III • __str__ is like toString() in Java and I use it
print the file: ... # inside of the last if # print the cursor in the right place if pos <= self.cursor <= pos + length: rv += " " * (self.cursor - pos) + "^\n" # inside of the for loop pos += length + 1 # last line in the function return rv
CS2304: Python for Java Programmers
Monti 2014
Creating And Using an Instance • Now we can stop using self:
>>> cf = CurrentFile() >>> cf.data ‘’ >>> cf.cursor 0 >>> cf.__str__() # will return the string >>> print(cf) # will print the file using __str__()
CS2304: Python for Java Programmers
Monti 2014
Class Methods
• Writing your own methods is basically the same. • Lets add change_cursor to CurrentFile. • Here’s a very simple definition:
• And this how you would use it:
def change_cursor(self, cursor_value): self.cursor = cursor_value
>>> cf = CurrentFile() >>> cf.cursor 0 >>> cf.change_cursor(100000) >>> cf.cursor 100000
CS2304: Python for Java Programmers
Monti 2014
Static Members
• Python supports static members that exist outside of an instance:
• Note: I’m using the class name, not an instance
name.
class hello: HELLO = “hello” def __init__(self): self.i = 1 j = 0
>>> hello.HELLO ‘hello’
CS2304: Python for Java Programmers
Monti 2014
Other Built-in Methods
• We’ve seen __init__ is the constructor. • __str__ converts the object into a human
readable string. • Here are some other built-in methods:
• __lt__, __gt__, __eq__, etc • __repr__ • __bool__
• You can override all of these if you want to use your class with Python operators, etc.
• If you bought the book Chapter 6 has some good examples.
CS2304: Python for Java Programmers
Monti 2014
Adding New Members
• Classes in Python are dynamic and it’s possible to add new members after creation:
class hello: HELLO = “hello” def __init__(self): self.i = 1 j = 0
>>> h = hello() >>> h.i 1 >>> h.k = ‘o’ >>> h.k
CS2304: Python for Java Programmers
Monti 2014
Public And Private Members • There really aren’t private members. • The convention is that variables and functions
that start with a _ or __ shouldn’t be touched. • If you use the double underscores Python will
make it harder to obtain by mangling the name:
class hello: def __init__(self): self.__i = 1
>>> h = hello() >>> h.i AttributeError: 'hello' object has no attribute 'i' >>> h._hello__i 1