[Tutor] Python's OOP-Inheritance make me confuse.
Hi, I am new to Python language. I have only 10 days experience on it. When I start learning there is no difficult, but it make me slow down when I reach "Object Oriented Concept", especially "Inherited". Some of my background knowledge about "Inherited is the child class can get all of characteristic and behaviour of parent class, in other words - data and methods of parent". Ok, I am going to show the concept that make me confuse with two programs. Both of them are getting the same result, so why people are making different. ---Frist-- class Parent(): parentdata = 0 def __init__(self): pass def getParentData(self): return Parent.parentdata def setParentData(self, setdata): Parent.parentdata = setdata class Child(Parent): childdata = 0 def __init__(self): pass def getChildData(self): return Child.childdata def setChildData(self, setdata): Child.childdata = setdata child = Child() print "Default Child's Data is :" + str(child.getChildData())#getting 0 child.setChildData(3) print "After Adding Child's Data is :"+ str(child.getChildData()) # getting 3 print "Default Parent's Data is:"+ str(child.getParentData())# getting 0 child.setParentData(1) print "After Adding Parent's Data is :"+str(child.getParentData())# getting 1 -Second- class Parent(): parentdata = 0 def __init__(self): pass def getParentData(self): return Parent.parentdata def setParentData(self, setdata): Parent.parentdata = setdata class Child(Parent): childdata = 0 def __init__(self): #super(Child, self).__init__() #super(Child, self).__init__(self, self) Parent.__init__(self) def getChildData(self): return Child.childdata def setChildData(self, setdata): Child.childdata = setdata child = Child() print "Default Child's Data is :" + str(child.getChildData())#getting 0 child.setChildData(3) print "After Adding Child's Data is :"+ str(child.getChildData()) # getting 3 print "Default Parent's Data is:"+ str(child.getParentData())# getting 0 child.setParentData(1) print "After Adding Parent's Data is :"+str(child.getParentData())# getting 1 --- And also guide me, how to use "Super()" method for instance of "Parent.__init__(self) Somebody used, Super method ih there and some are doing as my way. I am not clearly these two different. In these two program - I am not using "__init__" as constructor. If I am going to use "__init__" as to add data into the class's data(childdata, parentdata), how do I insert parameter in "Parent.__init__(self)" and both of their "def __init__(self):" method. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] HELP- Regarding working with python
On 1/18/2013 8:03 AM, eryksun wrote: Yes, it's a mistake in the PCA example from the docs: http://mlpy.sourceforge.net/docs/3.5/dim_red.html#principal-component-analysis-pca There seems to be no way to report a bug in that documentation! Or am I missing something? -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list of references to object properties
On 18/01/13 11:11, Jose Amoreira wrote: Suppose I have a list l_obj of similar objects. Is there any way I can generate a list l_prp of references to a given property of those objects in such a way that, if change the value of one element in l_prp, the corresponding object in l_obj gets its property updated, and vice-versa? Not easily, and for very good reason, it breaks one of the fundamental principles of OOP. Objects should manage their own data and clients should manage the objects. So getting a list of the prp values is fine. Trying to modify those values without going through the object is asking for trouble and creating a nightmare for debugging. So unless you have a very specific reason to do so just modify the objects prp through the object. As expected, changes in l_prp do not change the properties of the elements in l_obj, neither do changes in l_obj's element's properties change the values in l_prp. As you say, it's as expected and very worrying if it did otherwise! Is there a simple way to implement such connections? It's difficult by intent. It's to save you from yourself. Is there a good reason to want to do such a thing? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sqlite search syntax
Roger Shaw wrote: > > Hello, > I am very new to python. > > Wrote a small program to use on my android phone using pickle/shelve to > access data. > > That worked fine but i realised it would be better to use sqlite as a > database to more easily modify the data. > > I havent got a clue about sqlite, have a book but cant find the answer > > My problem is this. > > I can access data by putting characters to search for into the program but i > want it to be a variable string > that i can search for. > > Specificaly a couple of letters i input from keypad. > > > > At the moment this works to search for everything beginning with A > sql = "SELECT * FROM plants WHERE genus LIKE 'A%'"; > cursor.execute(sql); You should avoid the above style query if you ever get data from an untrusted source (user/internet) as bad things (obligatory xkcd: http://xkcd.com/327/ ) can happen. Instead, use parameterized queries which will handle escaping the input. sql = "SELECT * FROM plants where genus LIKE ?" cursor.execute(sql, (genus + '%')) # Add wildcard to parameter, not the base # query. Using this notation, genus can hold one character or any amount. > slt =cursor.fetchone(); > What i really need is to search for everything beginning with two letters > from an input command. > > As in A is a variable that could be Bl or An or someother two letter > combination > ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Set LD_LIBRARY_PATH and equivalents platform-independently
>> Thanks for your replies. os.putenv() may be easier than os.environ because, >> hopefully, it takes care of the OS-specific separators of the values >> (";" for Windows, ":" for Linux, others I don't > know > > I wouldn't count on it. Support for changing environment variables on the > fly is iffy in most languages and is not something I'd ever do lightly. > > The environment is something that usually should be set up when the user logs > in > to reflect their preferences, its not really polite of a program to try and > alter the users environment... If you put it that way... yes, I now realize that it could be (and in fact is) really annoying if programs do such things. ;-) I hate programs that nest themselves everywhere in my OS, using resources. RealPlayer used to be notorious in this respect: shell integration, auto-updater, quickstart, icons all over the place, waaah). >> Then again, this sentence from the Python page is a little worrying: >> "*If* the platform supports the putenv() function, ...". > > Not all OS allow changes to the environment variables. Others only allow the > value to be a fixed size allocated at startup and if you write more bytes > than > are already assigned you will overwrite the next value! The OS assumes that > this > stuff is set up once and not changed. > > putenv() should always be considered a platform specific bit of code. > > >> As an alternative, I used os.chdir to tell the OS where to start >> looking for libraries, but somebody on this list (I forgot who) >> warned me that this could have nasty side effects. > > Again the user may expect to be in a certain directory so as a minimum you > need > to change back to where they were when you exit. Yes, I considered first using oldpath = os.getcwd(), then use os.chdir in a try clause, and os.chdir(oldpath) in a finally clause. > The normal way to deal with local file locations is to have a resource file > that's read when the program starts up - like .vimrc or .emacs > Then if the file doesn't exist you either create it at install time or use > default values and allow the user to override them. You mean something like a setup.cfg that is read by ConfigParser? The coolest would be if it worked out-of-the-box, but this may be the next best thing. I want my C libraries and my .py files to live in the same dir (so not in a standard location such as /usr/lib, but in site-packages. Thank you Alan! Albert-Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] sqlite search syntax
Hello, I am very new to python. Wrote a small program to use on my android phone using pickle/shelve to access data. That worked fine but i realised it would be better to use sqlite as a database to more easily modify the data. I havent got a clue about sqlite, have a book but cant find the answer My problem is this. I can access data by putting characters to search for into the program but i want it to be a variable string that i can search for. Specificaly a couple of letters i input from keypad. At the moment this works to search for everything beginning with A sql = "SELECT * FROM plants WHERE genus LIKE 'A%'"; cursor.execute(sql); slt =cursor.fetchone(); What i really need is to search for everything beginning with two letters from an input command. As in A is a variable that could be Bl or An or someother two letter combination Hope you can help. Roger___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] HELP- Regarding working with python
On Fri, Jan 18, 2013 at 3:25 AM, Lie Ryan wrote: > On 18/01/13 17:11, Gayathri S wrote: >> >> >>> import numpy as np >> >>> import matplotlib.pyplot as plt >> >>> import mlpy >> >>> np.random.seed(0) >> >>> mean,cov,n=[0,0],[[1,1],[1,1.5]],100 >> >>> x=np.random.multivariate_normal(mean,cov,n) >> >>> pca.learn(x) >> Traceback (most recent call last): >>File "", line 1, in >> NameError: name 'pca' is not defined > > As in your particular problem, the basic issue is that you have not defined > 'pca' to refer to any object. You need to create an object named pca, which > in this particular case the object is probably an instance of mlpy.PCA, > which can be constructed like this: > > pca = mlpy.PCA() Yes, it's a mistake in the PCA example from the docs: http://mlpy.sourceforge.net/docs/3.5/dim_red.html#principal-component-analysis-pca ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list of references to object properties
Thanks, Peter. I was trying to avoid the regenerate step for updating. But maybe I can restructure my code and objects to make this simpler. Before that I'll try this view approach. Thanks again Ze On Fri, Jan 18, 2013 at 11:37 AM, Peter Otten <__pete...@web.de> wrote: > Jose Amoreira wrote: > > > Hello > > Suppose I have a list l_obj of similar objects. Is there any way I can > > generate a list l_prp of references to a given property of those objects > > in such a way that, if change the value of one element in l_prp, the > > corresponding object in l_obj gets its property updated, and vice-versa? > > Let give an example of what I have in mind. > > > > In [1]: class MyCls(object): > > > >...: def __init__(self,a): > > > >...: self.prp = a > > > >...: > > > > In [2]: l_obj = [MyCls(float(i)) for i in range(3)] > > > > In [3]: l_prp = [item.prp for item in l_obj] > > > > In [4]: for ob in l_obj: > > > >...: print ob.prp, > > > >...: > > > > 0.0 1.0 2.0 > > > > In [5]: l_prp > > > > Out[5]: [0.0, 1.0, 2.0] > > > > In [6]: l_prp[1]=5. > > > > In [7]: l_obj[1].prp > > > > Out[7]: 1.0 > > > > As expected, changes in l_prp do not change the properties of the > elements > > in l_obj, neither do changes in l_obj's element's properties change the > > values in l_prp. > > > > Is there a simple way to implement such connections? > > No. You'd need something like the observer pattern (listeners in Java), > where the class owning the property (MyCls) has to cooperate. The > administrative overhead is relatively high. > > The pythonic way is to regenerate the l_prp list every time you need an up- > to-date overview of the current values. > > An intermediate approach is to turn l_prp into a view on the l_obj list: > > from collections import Sequence > > class A(object): > def __init__(self, attrib): > self.attrib = attrib > > class AttribView(Sequence): > def __init__(self, items): > self._items = items > def __getitem__(self, index): > return self._items[index].attrib > def __len__(self): > return len(self._items) > def __repr__(self): > return repr(list(self)) > > items = [A(c) for c in "abcde"] > attribs = AttribView(items) > print attribs > items[1].attrib = 42 > print attribs > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list of references to object properties
Jose Amoreira wrote: > Hello > Suppose I have a list l_obj of similar objects. Is there any way I can > generate a list l_prp of references to a given property of those objects > in such a way that, if change the value of one element in l_prp, the > corresponding object in l_obj gets its property updated, and vice-versa? > Let give an example of what I have in mind. > > In [1]: class MyCls(object): > >...: def __init__(self,a): > >...: self.prp = a > >...: > > In [2]: l_obj = [MyCls(float(i)) for i in range(3)] > > In [3]: l_prp = [item.prp for item in l_obj] > > In [4]: for ob in l_obj: > >...: print ob.prp, > >...: > > 0.0 1.0 2.0 > > In [5]: l_prp > > Out[5]: [0.0, 1.0, 2.0] > > In [6]: l_prp[1]=5. > > In [7]: l_obj[1].prp > > Out[7]: 1.0 > > As expected, changes in l_prp do not change the properties of the elements > in l_obj, neither do changes in l_obj's element's properties change the > values in l_prp. > > Is there a simple way to implement such connections? No. You'd need something like the observer pattern (listeners in Java), where the class owning the property (MyCls) has to cooperate. The administrative overhead is relatively high. The pythonic way is to regenerate the l_prp list every time you need an up- to-date overview of the current values. An intermediate approach is to turn l_prp into a view on the l_obj list: from collections import Sequence class A(object): def __init__(self, attrib): self.attrib = attrib class AttribView(Sequence): def __init__(self, items): self._items = items def __getitem__(self, index): return self._items[index].attrib def __len__(self): return len(self._items) def __repr__(self): return repr(list(self)) items = [A(c) for c in "abcde"] attribs = AttribView(items) print attribs items[1].attrib = 42 print attribs ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] list of references to object properties
Hello Suppose I have a list l_obj of similar objects. Is there any way I can generate a list l_prp of references to a given property of those objects in such a way that, if change the value of one element in l_prp, the corresponding object in l_obj gets its property updated, and vice-versa? Let give an example of what I have in mind. In [1]: class MyCls(object): ...: def __init__(self,a): ...: self.prp = a ...: In [2]: l_obj = [MyCls(float(i)) for i in range(3)] In [3]: l_prp = [item.prp for item in l_obj] In [4]: for ob in l_obj: ...: print ob.prp, ...: 0.0 1.0 2.0 In [5]: l_prp Out[5]: [0.0, 1.0, 2.0] In [6]: l_prp[1]=5. In [7]: l_obj[1].prp Out[7]: 1.0 As expected, changes in l_prp do not change the properties of the elements in l_obj, neither do changes in l_obj's element's properties change the values in l_prp. Is there a simple way to implement such connections? Thanks, Ze ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] HELP- Regarding working with python
On 18/01/13 17:11, Gayathri S wrote: hi... I am using principal component analysis for dimensionality reduction in python. am having this following error... >>> import numpy as np >>> import matplotlib.pyplot as plt >>> import mlpy >>> np.random.seed(0) >>> mean,cov,n=[0,0],[[1,1],[1,1.5]],100 >>> x=np.random.multivariate_normal(mean,cov,n) >>> pca.learn(x) Traceback (most recent call last): File "", line 1, in NameError: name 'pca' is not defined >>> would you please help me in finding the error...? what was my fault? how could i use PCA in python..? You might want to start with a more basic tutorials for working with python, for example the official tutorial at http://docs.python.org/2/tutorial/ You should go through at least the first 5-6 chapters, which should take no more than several hours to skim through if you already have experience in any other languages. As in your particular problem, the basic issue is that you have not defined 'pca' to refer to any object. You need to create an object named pca, which in this particular case the object is probably an instance of mlpy.PCA, which can be constructed like this: pca = mlpy.PCA() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] HELP- Regarding working with python
On Thu, Jan 17, 2013 at 10:11 PM, Gayathri S wrote: > >>> import numpy as np > >>> import matplotlib.pyplot as plt > >>> import mlpy > >>> np.random.seed(0) > >>> mean,cov,n=[0,0],[[1,1],[1,1.5]],100 > >>> x=np.random.multivariate_normal(mean,cov,n) > >>> pca.learn(x) > Traceback (most recent call last): > File "", line 1, in > NameError: name 'pca' is not defined > >>> > would you please help me in finding the error...? what was my fault? how > could i use PCA in python..? > > The error means exactly what it says: you've referred to "pca", but you haven't told Python what "pca" is. I don't know the actual name of the PCA module you're using, but you need to import it the same way you've imported the other packages: - if it's called simply "pca", then just write "import pca" - if it has some other, slightly longer name, and you want to shorten it (as you did with "numpy", shortening it to "np"), then: "import longPCAname as pca" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor