Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,

Similar presentations


Presentation on theme: "Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,"— Presentation transcript:

1 Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation, but its interesting anyway. You can write nice OOP applications as long as you don't cheat.

2 Basic Class Syntax class Name: [body] Example with constructor
class Rectangle: def __init__(self,x,y): # parameterize constructor self.width=x #declare instance variable width self.height=y #declare instance variable height rect=Rectangle(2,5) # allocates rectangle instance print rect.width,rect.height #hmmmm

3 Class Variables class Rectangle:
count=0 #class variables declared here def __init__(self,x,y): Rectangle.count+=1 self.width=x self.height=y r= Rectangle(3,4) s= Rectangle(9,4) t= Rectangle(3,6) print Rectangle.count Outputs 3

4 Define output format of instance
class Complex: def __init__(self,r,i): self.real=r self.imag=i def __str__(self):#this is how it prints if self.imag<0: return str(self.real)+str(self.imag)+"i" else: return str(self.real)+'+'+str(self.imag)+"i" x=Complex(-1,-2) y=Complex(2,5) print x,y # uses __str__ to display the instances Output is -1-2i 2+5i

5 Add method for Complex class Complex: def __init__(self,r,i):
self.real=r self.imag=i def Add(self,c): return Complex(self.real+c.real,self.imag+c.imag) def __str__(self):#this is how it prints if self.imag<0: return str(self.real)+str(self.imag)+"i" else: return str(self.real)+'+'+str(self.imag)+"i" x=Complex(-1,-2) y=Complex(2,5) z=x.Add(y) print z Output is 1+3i

6 Destructors ?? Destructors are a big deal in C++ but not as much so in Python. Why? Well Python has automatic garbage collection using reference counting. This does not mean there is no need for destructors. You may want to clean up other allocated things like sockets and database connections to be closed, files, buffers and caches flushed and a few more resources that need to be released when an object is done with them.

7 Destructor Example class FooType(object): def __init__(self, id):
self.id = id print self.id, 'born' def __del__(self): print self.id, 'died' def make_foo(): print 'Making...' ft = FooType(1) print 'Returning...' return ft print 'Calling...' ft = make_foo() print 'End...' Note: Python won't clean up an object when it goes out of scope. It will clean it up when the last reference to it has gone out of scope. Here is its output Calling... Making... 1 born Returning... End... 1 died Note2: Don’t use destructors unless you really need to.

8 Longer Example import datetime class Person: def __init__(self,name):
self.name = name try: lastBlank = name.rindex(' ') self.lastName = name[lastBlank+1:] except: self.lastName = name self.birthday = None def getName(self): return self.name def setBirthday(self,birthdate): self.birthday = birthdate def getAge(self): if self.birthday == None: raise ValueError return (datetime.date.today()-self.birthday).days def __str__(self): return self.name me =Person('Thomas Adams') me.setBirthday(datetime.date(1956,1,31)) print me,'is',me.getAge(),'days old' Thomas Adams is days old


Download ppt "Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,"

Similar presentations


Ads by Google