Re: [Tutor] Python and memory allocation
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 on behalf of Dave Angel 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
[Tutor] python question
Hi! I am trying to do an exercise abt classes, but I find it really hard even after reading. I have tryed to start it at the bottom of this document. I hope someone can help me. I have to submit the question by sunday. Exercise 7.4. Make a class for straight lines. Make a class Line whose constructor takes two points p1 and p2 (2- tuples or 2-lists) as input. The line goes through these two points (see function line in Chapter 3.1.7 for the relevant formula of the line). A value(x) method computes a value on the line at the point x. Here is a demo in an interactive session: >>> from Line import Line >>> line = Line((0,-1), (2,4)) >>> print line.value(0.5), line.value(0), line.value(1) 0.25 -1.0 1.5 My answer: class Line: def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 def value(self, x): p1 = (y1 - y0)/float(x1 -x0) # Formula referred to in chapter 3.1.7 in the book p2 = (y0 - a*x0) # return def line (x0, y0, x1, y1): p1 = (x0 - x1) p2 = (y0 - y1) return p1, p2 Kindest Regards A.F. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python question
On 25/10/13 09:13, Amir Fawzi wrote: Exercise 7.4. Make a class for straight lines. Make a class Line whose constructor takes two points p1 and p2 (2- tuples or 2-lists) as input. The line goes through these two points (see function line in Chapter 3.1.7 for the relevant formula of the line). I don;t have the reference but assume it is one of the standard math equations like y=mx+c? So for your class you need to work out m and c and in your value() function apply them to the input x to return y. The trick is that you need to solve the simultaneous equations at p1 and p2 for y=mx+c. I assume you know how to do that in math and then translate that into Python? If not come back with more specific questions. >>> from Line import Line >>> line = Line((0,-1), (2,4)) >>> print line.value(0.5), line.value(0), line.value(1) 0.25 -1.0 1.5 My answer: class Line: def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 def value(self, x): p1 = (y1 - y0)/float(x1 -x0) # Formula referred to in chapter 3.1.7 in the book p2 = (y0 - a*x0) # return I'm guessing that last one should be p2 = y0 - p1*x0 ? But you want to calculate this in the init method and store them as attributes of the line. It might be nice to give them meaniungful names like gradient and, erm, const? Then your y value is just a case of multiplying x by the gradient(p1) and adding the const(p2). def line (x0, y0, x1, y1): p1 = (x0 - x1) p2 = (y0 - y1) return p1, p2 I'm not sure what this is supposed to be? HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] Count Help
It's better to use the "with open" statement to open the file and then process it line by line with occurrence counter, like: def occurence(pathToFile, substring): substringCount = 0 with open(pathToFile, 'r') as src: for line in src: substringCount += line.count(substring) return substringCount On 24 October 2013 17:01, Dave Angel wrote: > On 23/10/2013 22:12, Jackie Canales wrote: > >> let say i have a file with random letters of A, AB, C, CD, AC, A, D, CD, >> DD, C, B, AB, CD, AB >> How do i count the occurrence of each individual item. >> >> def occurence(name): >> infile = open('bloodtype1.txt', 'r') >> lst = infile.read() > > read() doesn't return a list, it returns a string. So the name is > poorly chosen. Probably you wanted a list, with each item containing > one or two letters. > > You can use split(",") to break the string into a list, based on commas. > And you can use strip() on each item of that list to get rid of > whitespace. > > >> infile.close() >> >> print(lst.count('AB')) # 3 >> print(lst.count('CD')) # 2 >> print(lst.count('DD'))# 1 >> >> >> i know i can do lst.count("AB") it will give me 3 but if i were to do >> lst.count("A") it would give me 6 what I am having trouble with is just >> getting the occurrence of the A by itself to give me 1 since there is just >> one occurrence of it and would need to do the same for c and d with out it >> counting the them in the occurrence of AC and DD or CD. > > When searching the original string, you have that problem. Making an > actual list will fix it. > > > -- > 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
Re: [Tutor] Python and memory allocation
On 25/10/2013 08:20, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: > 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. > For example, if you have a list of 100 strings, each 1000 bytes long, the total size of the list is 100,400+ bytes. If you make a new list, by using sorted_list = sorted(mylist) The new one is roughly 400+ bytes, since it doesn't make copies of any of the strings. (The + is intended to represent the overhead, which in some cases is substantial. And the factor of 4 is assuming a 32bit Python implementation) > > 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? > Variable a is an auto variable, created on the stack when the function is entered, and removed when the function returns. -- Signature file not found ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 3.3 on OpenSUSE 12.3
Hello Jay, thanks for your reply. On Thu, Oct 24, 2013 at 11:45 PM, Jay Lozier wrote: > On Thu, 2013-10-24 at 21:57 +0200, Rafael Knuth wrote: > > Hej, > > I can't get Python 3.3 up and running (it's properly installed but I > > can't launch it), and I was wondering if anyone using OpenSUSE 12.3 > > had similar issues. SUSE community folks weren't really able to help > > me, so I was wondering I give it a try here. > > Thanks, > > Rafael > > Rafael, > > I have both Python 2.7.3 and 3.3.0 installed on openSUSE 12.3. To use > 3.3 I enter python3.3 at the prompt. If I enter python it defaults to > 2.7.3 > > In the shebang #!/usr/bin/env python3.3 > Yes. > > -- > Jay Lozier > jsloz...@gmail.com > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor