Hi I am trying to call a function on change of an attribute of a class, how can i do this in Python?
-
-
Answer:
Going off of the following in the comment section: Will post the example in some time...for now..assume there is a attribute date, so when i set it to todays date..another attribute year should be automatically populated to year of the date.. There are a couple of ways of doing this, but the obvious ones involve properties. Properties combine accessor (getter), mutator (setter) and destructor methods of an object into a single sub-object. Or, to put it another way, properties combine a collection of methods into an interface that looks like a single public variable. So: class SampleClass(object): def __init__(self, sample_date): self.sample_date = sample_date @property def sample_date_year(self): return self.sample_date.year if __name__ == '__main__': from datetime import date sc = SampleClass(date(2013, 1, 1)) print(sc.sample_date_year) sc.sample_date = date(2112, 1, 1)) print(sc.sample_date_year) >>> 2013 >>> 2112 Notice how even though sample_date_year looks like a method of SampleClass, we don't actually have to call it to get the year's output. This is certainly a workable solution - but you'll notice it still involves referencing sample_date.year every time we access sample_date_year. This really shouldn't be an issue, but let's say you're really trying to cut down on lookups because you desperately (cannot emphasize the word "desperately" enough) need higher performance. What you can do is start messing around with mutators, as well: class SampleClass(object): def __init__(self, sample_date): self.sample_date = sample_date @property def sample_date(self): return self._sample_date @sample_date.setter def sample_date(self, sample_date): self.sample_date_year = sample_date.year self._sample_date = sample_date This second way is, in my opinion, overkill for the problem at hand, but there are other problems where it may be the obvious choice. Here's the documentation, check it out: http://docs.python.org/2/library/functions.html#property
Harold Kingsberg at Quora Visit the source
Other answers
You mean something like this? class dateclass(object): def __init__(self,s=None): self.date=s or '01-01-1984' self.year=self.date[self.date.rfind('-')+1:] def setdate(self, newdate): self.date=newdate self.year=newdate[newdate.rfind('-')+1:] def printcontents(self): print "date = ",self.date print "year =",self.year instance1=dateclass("12-12-2007") instance1.printcontents() instance1.setdate('12-12-2011') instance1.printcontents() print"------------" instance2=dateclass("12-12-2042") instance2.printcontents() instance2.setdate('12-12-2084') instance2.printcontents() print"---------" instance3=dateclass() instance3.printcontents() If you want to access the attributes directly without the setter methods use properties like this: class dateclass(object): def __init__(self,s=None): self._date=s or '01-01-1984' self.year=self._date[self._date.rfind('-')+1:] def setdate(self, newdate): self._date=newdate self.year=newdate[newdate.rfind('-')+1:] def getdate(self): return self._date def printcontents(self): print "date = ",self._date print "year =",self.year date=property(getdate,setdate) #look up documentation for this instance1=dateclass("12-12-2007") instance1.printcontents() instance1.date='12-12-2012' #notice not using setter instance1.printcontents()
Aritra Das
Related Q & A:
- How can I pass global variables into a function?Best solution by Stack Overflow
- I want to change my cellphone number on Yahoo mobile. How can I do this.Best solution by Yahoo! Answers
- I have a blog how can I make money with it.Best solution by Yahoo! Answers
- How can I make a phone call to a cruise ship?Best solution by traveltips.usatoday.com
- I was trying to send a e-mail and it did not send how can i find it again.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.