Hello All, >Similarly, if you call sorted() on a list of large strings, you get a new list, but the strings are not duplicated, so it's not nearly the duplication it might look like.
1. Sorry, but I did not understand the above point. 2. My interpretation of your answer is that the stack memory in Python holds only pointers that hold references to data on the heap. Is this correct? 3. Also, when you spoke about the second type of variable lifetime in C, did you mean the local scope of a variable like: int func(){ int a = 5; printf("a"); return 0; } Here were you talking about 'a' which is removed from the stack when it goes out of scope? Thanking You, Jatin ________________________________________ From: Tutor <tutor-bounces+jatinshr001=e.ntu.edu...@python.org> on behalf of Dave Angel <da...@davea.name> Sent: Friday, October 25, 2013 01:21 AM To: tutor@python.org Subject: Re: [Tutor] Python and memory allocation On 24/10/2013 12:38, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: > Hello All, > > Before starting to learn python, I first learnt C and C++ for a couple of >years. In C/C++, the choice to assign memory during compile time or during >execution time, i.e. assigning memory from the stack or the heap lay with the >programmer right? But in Python, I have only seen examples of using heap >memory in programs. Like list.append(x) and so on. So, is there a concept of >heap and stack in python or is it abstracted? And does python being an >interpreted language have to do anything with this? > > > > Thanks and sorry for the newbie doubt :) > That's what this group is for, welcome. In C and C++, there are 3 kinds of variable lifetime, rather than 2. If a variable is static, it is created and initialized before your program really starts, and lasts till the program goes away. If a variable is automatic (stack-based), it will be created when the function (or even the scope within a function) begins, and destroyed when the function or scope ends. And this is especially important in C/C++ because when a function is recursive, there may be many named variables on the stack with the same name, and independent values. If a data item is heap-based, it is not really a variable at all, but is a block created by malloc() or equivalent, and *pointed to* by a variable. And it's freed when the corresponding free() or equivalent is called. In Python it's all different. There aren't really any static variables that the user defines, though there are plenty of variables that are defined and initialized long before your code starts running. If you want a surprise, try the following simple program some time. import sys print(sys.modules) when I tried that interactively on 2.7, it printed some 240+ names. As for auto versus heap-based, all values you define are both. What's stored in the 'stack' is references to objects, where the objects are allocated on the heap. And those in turn may have references to other objects, and so on. So if you have a variable x which is a list of size 10, then you have a refernce to a list object, which in turn has references to ten other objects. All these objects are reference counted, and when the count reaches zero, they vanish. And for circular links, there's a garbage collector which operates periodically. These references are referred to as bindings in the python literature. The real point is that when you use a value, you don't have to worry about where it'll be stored, but just on how you'll find it again when you need it. You may have a name bound to it, and also a list item. Even if you reuse the name, the list item will still assure you can find it again. Similarly, if you call sorted() on a list of large strings, you get a new list, but the strings are not duplicated, so it's not nearly the duplication it might look like. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor