[Tutor] inquiry
| Dear tutors: I have some questions about memory mechanism of python. Do the elements of the parent go into the namespace of the subclass when we define subclass? As far as I'm concerned, they do go into the namespace of the subclass. Because when we use dir(), we can see all the elements. But there is only one 'update' name. I think the function of the subclass override the parent's. In this situation , when we instantiate the subclass, we just need one argument, that is to say, the parent's update function do the work. It confuses me. According to my understanding, as there is no 'super' key word, the 'self' should inference to the instance of the subclass when we initiate the subclass. So self.__update should inference to function of the subclass. Another quesion is about the meaning of the private variables. What condition shall we use them? In other word, I wanna know the purpose of introducing the private variables. The above-mentioned example, I think, is not enough to explain it, because when we change the __update to update, there is no change except for using update rather than _Mapping__update to inference it. The last question is about the storage of the data. For a given data, like 1 or '1', what's the form when python saves it in the memory? To be more specific, I wanna know the number of the parts we use to enclose it. For example, python uses 3 parts to save it. One is for the value. The second for the type of the data, ie, inter,str,etc. The last is for the pointer or id to show the place where it is stored. Hope to hear from you soon. Have a good weekend! Yours, sincerely. Phoenix | ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with Python Queue
I was just writing to read a file using popen and wanted to use queue along with it. I have below code but it is giving this error: AttributeError: Queue instance has no attribute 'taskdone' import threading from Queue import Queue def worker(q): while True: dataset = q.get() print("q is taken out") q.taskdone() def create_data(q): print("Inside create data") output = [1, 2, 3] for i in output: print("Inside", i) print("queue is put") q.put(i) if __name__ == '__main__': q = Queue() t = threading.Thread(target=worker, args=(q,)) t.daemon = True t.start() create_data(q) q.join() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] inquiry
On 26/11/16 04:59, zxjhust1 wrote: > Dear tutors: >I have some questions about memory mechanism of python. Depending on the level of detail you need this might be more appropriate to the general Python list. Implementation details are usually a bit too advanced for most readers of tutor. However if all you want are the practical details of how to utilise the memory model this list is fine. > Do the elements of the parent go into the namespace of > the subclass when we define subclass? > > As far as I'm concerned, they do go into the namespace > of the subclass. Because when we use dir(), > we can see all the elements. > But there is only one 'update' name. I'm not sure what you mean by that? Where are you getting 'update' from? > I think the function of the subclass override the parent's. Yes that's the expected behaviour. > In this situation , when we instantiate the subclass, > we just need one argument, that is to say, > the parent's update function do the work. I think you need to post some code because what you are talking about sounds like its related to a specific example which we can't see... > According to my understanding, as there is no 'super' key word, There is a form of super in Python although it works differently in v2 and v3. Which python version are you using? And both cases work differently to Java and Smalltalk. > Another quesion is about the meaning of the private variables. Again you need to be more specific. Private attributes in Python are a bit of a bodge, more of a convention than a guarantee. > What condition shall we use them? In other word, I wanna > know the purpose of introducing the private variables. Are you familiar with OOP in other languages? That will affect how we answer that question... I have to run, hopefully someone else will picjk this up in more detail. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] inquiry
Hi Phoenix, welcome! On Sat, Nov 26, 2016 at 12:59:37PM +0800, zxjhust1 wrote: > I have some questions about memory mechanism of python. Do the > elements of the parent go into the namespace of the subclass > when we define subclass? I don't quite understand your question. I *think* you are asking if the parent's attributes are copied into the subclass. The answer to that is no, they are not: py> class Parent: ... member = 'leg' ... py> class Child(Parent): ... pass ... py> print(Child.member) leg py> print('member' in vars(Child)) False The vars() function returns the namespace of the argument, so there is no attribute "member" inside the Child namespace, instead the attribute is looked up when needed. > As far as I'm concerned, they do go into the namespace of the > subclass. Because when we use dir(), we can see all the elements. But > there is only one 'update' name. I think the function of the subclass > override the parent's. In this situation , when we instantiate the > subclass, we just need one argument, that is to say, the parent's > update function do the work. It confuses me. Your question confuses me. What "update" name? Can you show a SMALL example that explains what you are talking about? The dir() function shows attribute and method names of the object, its class, and its parent classes. > According to my understanding, as there is no 'super' key word, the > 'self' should inference to the instance of the subclass when we > initiate the subclass. So self.__update should inference to function > of the subclass. I don't know what you mean by "self.__update" here, but there is an added complication when the method name starts with two underscores: name mangling. The interpreter will insert the name of the class at the front of the method. Name mangling is intended as a simple form of method protection, but in my experience it causes more problems than it solves. I'm happy to talk about name mangling more if you like, but as a beginner you will probably be much happier if you forget all about it until you have more experience. There is no "super" keyword, but there is a super() function. But beware: in Python 2 it is complicated to get right. Suppose I say: class Animal: def grow(self): print(self, 'is growing') class Mammal(Animal): pass class Dog(Mammal): pass lassie = Dog() # an instance lassie.grow() then I get something like this: <__main__.Dog object at 0xb79d0a4c> is growing So it doesn't matter which class the method is actually defined in, the instance is still a Dog instance. > Another quesion is about the meaning of the private variables. What > condition shall we use them? In other word, I wanna know the purpose > of introducing the private variables. The above-mentioned example, I > think, is not enough to explain it, because when we change the > __update to update, there is no change except for using update rather > than _Mapping__update to inference it. In some languages, like Java, programmers make a big deal about private variables. The languages enforces them as private, and then programmers typically spend hours trying to fight the language to bypass the protection. In Python, private variables are not enforced by the language. They are just a naming convention: if you name a method or attribute with a single leading underscore, like _update(), that is a sign to other people "Don't use this". It means that you reserve the right to remove the _update() method with no warning, or make it do something else. In short, as a Python programmer using classes or functions from a library, you should NEVER use anything that starts with an underscore. As a Python programmer, the only time you might use something with an underscore is if you are writing your own classes. > The last question is about the storage of the data. For a given data, > like 1 or '1', what's the form when python saves it in the memory? To > be more specific, I wanna know the number of the parts we use to > enclose it. For example, python uses 3 parts to save it. One is for > the value. The second for the type of the data, ie, inter,str,etc. The > last is for the pointer or id to show the place where it is stored. That depends on which Python interpreter you are using, and which version of Python. But all values in Python are objects. How the object is defined in memory will depend on which interpreter, which version, and which object, but they would usually have at least two internal fields: - the type of the object (an int, float, str, list, dict, etc) - the value of the object (depends on what type it is). You cannot directly access these fields from Python code. You can get the type of the object with the type() function: py> type(lassie) # remember this from before? py> type(42) py> type("hello world") Hope these answer your questions. Regards, Steve _
Re: [Tutor] Help with Python Queue
On 26/11/16 09:07, anish singh wrote: > I have below code but it is giving this error: > AttributeError: Queue instance has no attribute 'taskdone' Please post the full error not just a summary. Also please post the actual code... > import threading > from Queue import Queue I get an import error here with no module named Queue. Which Queue module are you using? Or should it be spelled queue? > def worker(q): > while True: > dataset = q.get() > print("q is taken out") > q.taskdone() Assuming you meant the standard queue module then that should be q.task_done() When you get an attribute error it's worth trying dir() on the offending object: >>> from queue import Queue as Q >>> dir(Q) , task_done, >>> help(Q.task_done) ... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with Python Queue
> AttributeError: Queue instance has no attribute 'taskdone' Method name is task_done (not taskdone) https://docs.python.org/2/library/queue.html#Queue.Queue.task_done ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with Python Queue
>I have below code but it is giving this error: >AttributeError: Queue instance has no attribute 'taskdone' >import threading >from Queue import Queue >def worker(q): >while True: > dataset = q.get() > print("q is taken out") > q.taskdone() Per documentation it is task_done (I suppose you are using python 3+) https://docs.python.org/3.5/library/queue.html This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor