[Tutor] Mastering the fundamentals
Hello Tutorians, I've just recently acquired "Learning Python", and I must state that it is a fairly thorough book. However it seems as if I am learning at a very slow pace, so my question is, as far as setting a goal to master the basics, where should I be within a years time? Assuming I spend at least 2 hours of actual coding time per day. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] python 3.3 split method confusion
Hello fellow tutors, I am curious to know why the split() method does not output the arbitrary delimiter that is passed as an argument? For example: string1 = "this,is,just,another,string" print(string1.split(",")) I understand the the above code simply states, "break at every ' , ' ". But why is the delimiter not printed as well? -- Regards, Christian Alexander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python 3.3 split method confusion
Thank you for clarifying my inquiry. I was just unable to find the reason as to why the built-in excludes the delimiter from the outpu. On Sat, Jan 4, 2014 at 9:25 AM, Alan Gauld wrote: > On 04/01/14 14:10, Christian Alexander wrote: > > I am curious to know why the split() method does not output the >> arbitrary delimiter that is passed as an argument? For example: >> > > Because in most cases you don't want it and would have to strip > it off each element manually after the event. > > I suppose they could have had a preserve parameter with a > default value of False for the few cases where you want to > keep it. > > But in the majority of cases split is used where we read a line > of input from a data file where the data fields are separated > by some arbitrary character, usually comma, tab or pipe. The > important bit is the data not the separator. > > > -- > 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 > -- Regards, Christian Alexander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python 3.3 split method confusion
That makes total sense now. I was just curious as to why it didn't output the arbitrary delimiter in the list, or if there was a specific reason for it. On Sat, Jan 4, 2014 at 10:03 PM, Danny Yoo wrote: > One of the common cases for split() is to break a line into a list of > words, for example. > > # > >>> 'hello this is a test'.split() > ['hello', 'this', 'is', 'a', 'test'] > # > > The Standard Library can not do everything that we can conceive of as > being useful, because that set is fairly large. > > If the Standard Library doesn't do it, we'll probably need to do it > ourselves, or find someone who has done it already. > > > ## > >>> def mysplit(s, delim): > ... start = 0 > ... while True: > ... index = s.find(delim, start) > ... if index != -1: > ... yield s[start:index] > ... yield delim > ... start = index + len(delim) > ... else: > ... yield s[start:] > ... return > ... > >>> list(mysplit("this,is,a,test", ",")) > ['this', ',', 'is', ',', 'a', ',', 'test'] > ## > -- Regards, Christian Alexander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Interactive escape sequences
Hello Tutorians, Why does the interactive prompt not recognize escape sequences in strings? It only works correctly if I use the print function in python 3. >>> "Hello\nWorld" "Hello\nWorld" -- Regards, Christian Alexander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Understanding Classes
Hello Tutorians, Looked all over the net for class tutorials Unable to understand the "self" argument Attempting to visual classes I have searched high and low, for easy to follow tutorials regarding classes. Although I grok the general concept of classes, I am unable to visually understand what exactly "self" does, or why it is even necessary. It seems very "magic" to me. Also I am having the most difficult with the "__init__()" method in classes, and why that is also required. Keep in mind that I am a visual person (maybe I should have been a graphic designer), therefore most programming concepts flow irritatingly slow for me. -- Regards, Christian Alexander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding Classes
I would first like to state two things, those being that I am a horrible writer as well as explaining things, but Ill try my absolute best. Everything python is an object. Strings, integers, lists, so on and so forth. In regards to classes and their relativity towards objects, I am at complete standstill. However, I understand that classes are parallel to that of a blueprint, but that is unfortunately where the buck stops. On Sun, Jan 19, 2014 at 6:50 PM, Alan Gauld wrote: > On 19/01/14 21:59, Christian Alexander wrote: > >> Looked all over the net for class tutorials >> Unable to understand the "self" argument >> Attempting to visual classes >> > > If you read my OOP tutorial there is a section there specifically about > self. > > And the v3 tutor includes an introduction to the formal visualisation > technique for OOP called UML. The diagrams illustrating the designs may > help. > > http://www.alan-g.me.uk/l2p/tutclass.htm > > > I have searched high and low, for easy to follow tutorials regarding >> classes. Although I grok the general concept of classes, >> > > Do you also grok the concept of objects? > Classes on their own are fairly useless (unless you are using Java) > it is only when you create a universe of objects from those classes that > they become useful. > > If you can repeat to us your understanding of classes and their > relationship with objects that will help us understand your > level and shape our responses accordingly. > > > to visually understand what exactly "self" does, or why it is even >> necessary. It seems very "magic" to me. >> > > When you define a class you define the data (attributes) that > the class instances will have. Each instance will have a copy of the data > defined in the __init__() method. > You also define a set of operations or methods that are associated > with the class. Those methods are shared by the instances. > > Note the difference. Instances get a copy of the attributes > but they all share the methods. > > Thus when you invoke a method on an instance the instance relays that call > to the class. For the class to know which instance is being operated on, > and for the method to be able to access the correct instance's data it > needs a reference to the instance. That reference > is typically called 'self' or 'this'. (In some languages it's fixed > but in Python self is only a convention, you can use any name you like). > > You can make the call to the class explicit and it will still work. > See below: > > # define a class > class MyClass: > def __init__(self,x): self.x = x > def myMethod(self): print(self.x) > > # create some instances > ObjA = MyClass(2) > ObjB = MyClass(4) > ObjC = MyClass(6) > > # send some messages/call methods > objA.myMethod() # call from the instance > MyClass.myMethod(ObjB) # call explicitly to the class > objC.myMethod() # direct again > > All 3 calls do the same thing except the middle one > passes the object identifier directly to the class > whereas the first and last both do that internally > within the object structure. > > > difficult with the "__init__()" method in classes, >> > > and why that is also required. > > It is not *required* as such. You can create a class > without an init but it's unusual. > > When you create an instance of a class it creates a > data structure in memory referenced by the name of > the instance. But that structure is empty, it has > no data. So to populate the data for the instances > you must initialize it. That's what __init__() does. > It takes the arguments you provide and applies them > to the instance along with any static data definitions > you may define. > > In the example we create an instance variable, x, > within the instances and assign the value of the > argument passed to init. Like any other method the > actual code lives in the class so we could initialize > it by calling init like so: > > MyClass.__init__(objC, 66) > > which is almost the same as doing: > > objC = MyClass(66) > > The difference is that the first case requires the object ObjC > to already exist, the second example creates a new instance and > then calls init on that instance. > > > Keep in mind that I am a visual person (maybe I should have >> been a graphic designer), therefore most programming concepts flow >> irritatingly slow for me. >> > > Most programming concepts have visual representations, > its just that program code being text tends to lead programmers > to be verbally based. But algorithms, state machin
Re: [Tutor] Understanding Classes
Alan, The concept and purpose of classes is starting to sink in a little bit, but I still haven't had my "Ah-ha" moment yet. I just can't seem to visualize the execution of classes, nor am I able to explain to myself how it actually works. For example: class Person: def __init__ (self, name, age):# is self just a placeholder for an arbitrary object? How does __init__ actually work? self.name = name # why assign name to self.name variable? self.age = age # same as previous def salute (self): # again with the self print ("Hello, my name is " + self.name + " and I am " + str(self.age) " years old.") On Mon, Jan 20, 2014 at 4:20 PM, spir wrote: > On 01/19/2014 10:59 PM, Christian Alexander wrote: > >> Hello Tutorians, >> >> Looked all over the net for class tutorials >> Unable to understand the "self" argument >> Attempting to visual classes >> >> I have searched high and low, for easy to follow tutorials regarding >> classes. Although I grok the general concept of classes, I am unable to >> visually understand what exactly "self" does, or why it is even necessary. >> It seems very "magic" to me. Also I am having the most difficult with >> the >> "__init__()" method in classes, and why that is also required. Keep in >> mind that I am a visual person (maybe I should have been a graphic >> designer), therefore most programming concepts flow irritatingly slow for >> me. >> > > Imagine that for an app you had to define 2 persons p1 & p2 (maybe game > characters for instance). In an imaginary programming language, a > definition of p1 could look like this: > > p1 = {name="Maria", age=33} # no good python code > > This would be a composite piece of data, made of 2 fields (attributes, > properties...). In python there is no such generic type Object or Composite > to which such data as p1 could belong. You must define a custom type > (class) for them, eg: > > class Person: pass > > Now, you can have p1 of type Person, which is written as if you would call > the type Person, like a func, to make a new person (this is close to what > happens): > > p1 = Person() > > Then, one can define fields on it: > > p1.name = "Maria" > p1.age = 33 > print(p1.name, p1.age) > > We could do the same thing for p2: > > p2 = Person() > p2.name = "paulo" > p2.age = 22 > print(p2.name, p2.age) > > Now, say persons are supposed to do things, and all can do the same > things. To define something all persons can do, you would define it on > their class (this is the second purpose of a class), eg: > > class Person: > def salute (self): > print ("Hello, my name is " + self.name + > " and I am " + str(self.age) " years old.") > > As you can see, this method uses the attributes 'name' & 'age' we manually > defined on both p1 & p2. Then, how does the method, which is defined on the > type, not on individual objects, know where to find these attributes? You > are right to say there is some magic at play here. Let us use the method > first, before explaining: > > p1.salute() > p2.salute() > > [Copy-paste & run all this code.] On the first call, we ask the method > 'salute' to operate on p1, and it writes p1's name & age. Same for p2. > Inside the method, the attributes are searched on the weird param called > 'self'. This is just what happens: when calling a method, the object on > which it operates is assigned to the parameter self. 'self' is just a name > for the-object-on-which-this-method-operates-now. When it operates on p1, > self is p1, thus attributes are searched on p1; same for p2. We need some > placeholder because the method is defined on the type and works for any > object of this type (any "instance"). [We can define a method on p1 which > works on p1 only. Maybe try it.] > > Finally, if we define a whole range of persons that way, it is annoying to > set all attributes manually. We could define a method, say 'def_attrs', to > be called at startup. But python has a specially dedicated method for that, > which we don't even need to call explicitely, named '__init__'. We will use > it to set person attributes: > > class Person: > def __init__ (self, name, age): > self.na