Presentation is loading. Please wait.

Presentation is loading. Please wait.

From datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second.

Similar presentations


Presentation on theme: "From datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second."— Presentation transcript:

1 from datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second self.micro = now.microsecond def converttomus(self): return self.micro+self.sec*1000000+self.min*60*1000000+self.hr*60*60*1000000 def time_diff(self,watch): x = self.converttomus() y = watch.converttomus() diff = abs(x - y) return(diff) time1 = StopWatch() print time1.micro time2 = StopWatch() print time2.micro print(time1.time_diff(time2)) Class Stopwatch?

2 Private Attributes and Methods Private Attributes and methods (functions): Only other methods of the object can access or invoke these attributes/methods In our stopwatch class, we might want to make converttomus a private method So only other methods within the class can use it – there really isn’t a reason for anything else to use that method or for the object to use that method directly.

3 Private methods and attributes class Critter(object): """A virtual pet""" def __init__(self, name, mood): print("A new critter has been born!") self.name = name # public attribute self.__mood = mood # private attribute def talk(self): print("I'm“ + self.name) print("Right now I feel“ + self.__mood) def __privatemethod(self): print("This is a private method.") def publicmethod(self): print("This is a public method.") self.__privatemethod() # main crit = Critter(“Poochie", "happy") crit.talk() crit.public_method() print(crit.name) __________________________________________________________________________ Can we do print(crit.__mood)???? print(crit.mood) ???? crit.__privatemethod() ??? crit.privatemethod() ???

4 Example: class Checkbook(object): """A virtual pet""" def __init__(self, name, balance): print("Thanks for opening an account " + name) self.name = name # public attribute self.__balance = balance # private attribute def addmoney(self, amount): self.__balance += amount self.printbalance() def __warning(self,num): print("Warning: This transaction will overdraw your account by " + str(num) + ". Thus it has not been completed.") def spendmoney(self, amount): if amount > self.__balance: self.__warning(amount-self.__balance) else: self.__balance -= amount self.printbalance() def printbalance(self): print(“Your current balance is “ + self.__balance) # main myckbook = Checkbook(“Harriet Smith”, 3.25) myckbook.addmoney(6.75) #??? print(myckbook.__balance) #??? myckbook.printbalance() #??? myckbook.spendmoney(11.25) #??? myckbook.__warning(1.25) #??? __________________________________________________________________________

5 from datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second self.micro = now.microsecond def __converttomus(self): return self.micro+self.sec*1000000+self.min*60*1000000+self.hr*60*60*1000000 def time_diff(self,watch): x = self.__converttomus() y = watch.__converttomus() diff = abs(x - y) return(diff) time1 = StopWatch() print time1.micro time2 = StopWatch() print time2.micro print(time1.time_diff(time2)) #What if we did: print time1 Remember Class Stopwatch?

6 Printing an object time1 = StopWatch() print time1.micro time2 = StopWatch() print time2.micro print time1 Results? Probably not what we want we probably want to print out a StopWatch object’s properties (hour, minute, second, microsecond)

7 Creating String: class StopWatch: def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second self.micro = now.microsecond def __str__(self): vstr = "The time was: " amflag = True; if (self.hr > 12): vstr += str(self.hr - 12) amflag = False else: vstr += str(self.hr) vstr += ":" if self.min < 10: vstr += "0" vstr += str(self.min) if (amflag): vstr += " a.m. " else: vstr += " p.m. " vstr += str(self.sec) vstr += " seconds and " vstr += str(self.micro) vstr += " microseconds." return(vstr) #main time1 = StopWatch() print time1


Download ppt "From datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second."

Similar presentations


Ads by Google