2012-02-19 16 views
4

我學習蟒蛇期運用如何看待一個計算機科學家:學習Python 3Python3 /類/ OOP /你如何用一種方法改變對象的自我價值?

我學OOP,並把一些代碼來回答書中的一個問題,但我覺得我應該有做了別的事情。

有問題的代碼是incremental(),其目標是提高對象的價值。現在我的最終解決方案是讓我的方法成爲初始化方法的一個副本,並在那裏添加時間。

這種感覺馬虎:

class MyTime: 

    def __init__(self, hrs=0, mins=0, secs=0,): 
     """ Create a new MyTime object initialized to hrs, mins, secs. 
      The values of mins and secs may be outside the range 0-59, 
      but the resulting MyTime object will be normalized. 
     """ 

     # calculate total seconds to represent 
     totalsecs = hrs*3600 + mins*60 + secs 
     self.hours = totalsecs // 3600  # split in h, m, s 
     leftoversecs = totalsecs % 3600 
     self.minutes = leftoversecs // 60 
     self.seconds = leftoversecs % 60 

    def incerment(self,t): 
     # increase the time by t amount 
     totalsecs = self.hours * 3600 + self.minutes * 60 + self.seconds + t 
     self.hours = totalsecs // 3600  # split in h, m, s 
     leftoversecs = totalsecs % 3600 
     self.minutes = leftoversecs // 60 
     self.seconds = leftoversecs % 60 


t1 = MyTime(5,5,5) 
t2 = MyTime(10,10,10) 
t3 = MyTime(12,12,12) 

print('before:',t1) 
t1.incerment(100) 
print('after:',t1) 

那麼怎麼樣了?
有沒有辦法清理這個?

回答

4

那種感覺像你應該做別的事情這是怎麼一回事,因爲hoursminutesseconds性能

你並不真正需要存儲的對象的屬性這些值,你只是希望能夠當你需要訪問這些值。

調用類似:

>>> t1.hours 
5 

所以,讓我們使用property重寫你的榜樣:

class MyTime: 
    def __init__(self, hrs=0, mins=0, secs=0): 
     self.totalsecs = hrs*3600 + mins*60 + secs 

    @property 
    def hours(self): 
     return self.totalsecs // 3600 

    @property 
    def minutes(self): 
     return self._get_leftoversecs() // 60 

    @property 
    def seconds(self): 
     return self._get_leftoversecs() % 60 

    def _get_leftoversecs(self): 
     return self.totalsecs % 3600 

    def increment(self, t): 
     self.totalsecs += t 

用例:

>>> t1 = MyTime(5,5,5) 
>>> t1.hours 
5 
>>> t1.hours() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'int' object is not callable 
>>> t1.seconds 
5 
>>> t1.totalsecs 
18305 
>>> t1.increment(10) 
>>> t1.seconds 
15 
>>> t1.totalsecs 
18315 

我不知道,如果你注意到但是y OU實際上並不需要的功能increment

>>> t1.totalsecs += 10 
>>> t1.totalsecs 
18325 

我知道property必須是有點超前的你在做什麼,但我認爲這將是值得學習的榜樣。

編輯:由於Lattyware注意到有沒有必要使totalsecs屬性太。

引用他的評論:關於Python屬性的好處是你不需要把所有的東西都變成getter和setter來保持一致的界面,就像你在某些語言中一樣。

有可能是在設置totalsecs作爲一個屬性的優勢(只讀)僅如果由於某種原因,你要隱藏的內部實現MyTime(顯然重新整合increment()方法)。

+2

在所有情況下都同意,除了將''totalsecs''轉換成屬性 - 它的功能與普通屬性完全相同,所以爲什麼要放入額外的函數呢?除非需要更改它,否則請將其作爲屬性保留。關於Python屬性的好處是你不需要把所有東西都變成getter和setter來保持一致的界面,就像你在某些語言中一樣。 – 2012-02-19 11:57:59

+0

@Lattyware:好點。我剛剛得到了一些財產:)我已經更新了我的答案。 – 2012-02-19 15:00:03

0

主要的變化是你應該在__init__中使用incerment,因爲你的init包含基本相同的代碼。

1

你可以試試這個;

# calculate total seconds to represent 
    self.totalsecs = hrs*3600 + mins*60 + secs 
    self.set_times() 

def incerment(self, t): 
    # increase the time by t amount 
    self.totalsecs = self.hours * 3600 + self.minutes * 60 + self.seconds + t 
    self.set_times() 

def set_times(self): 
    self.hours = self.totalsecs // 3600  # split in h, m, s 
    leftoversecs = self.totalsecs % 3600 
    self.minutes = leftoversecs // 60 
    self.seconds = leftoversecs % 60 
0

我可能會分解出獲得「totalsecs」到屬性,然後使用:

class MyTime: 

    def __init__(self, hrs=0, mins=0, secs=0,): 
     """ Create a new MyTime object initialized to hrs, mins, secs. 
      The values of mins and secs may be outside the range 0-59, 
      but the resulting MyTime object will be normalized. 
     """ 
     self.totalsecs = hrs*3600 + mins*60 + secs 

    @property 
    def totalsecs(self): 
     return self.hours * 3600 + self.minutes * 60 + self.seconds 

    @totalsecs.setter 
    def totalsecs(self, totalsecs): 
     self.hours = totalsecs // 3600  # split in h, m, s 
     leftoversecs = totalsecs % 3600 
     self.minutes = leftoversecs // 60 
     self.seconds = leftoversecs % 60 

    def increment(self,t): 
     # increase the time by t amount 
     self.totalsecs += t 

當然,如果你那樣做,你並不真的需要increment()方法了。 如果您稍後決定切換到Rik Poggi的解決方案並存儲totalsecs值,並將小時,分鐘和秒作爲屬性,而不是最小更改,那麼還需要加上其他參數。

相關問題