[Tutor] class to function
Hi guys i am trying to understand this code: http://nbviewer.ipython.org/gist/BenLangmead/6665861 i understand functions quite alright . but i have no idea about classes yet. the code is written using class and i could not make much sense out of it. all this init and self thingy drive me crazy. can you guys help explain. super thanks -- many thanks mat ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class to function
can you guys help explain. super thanks A class is like a blueprint. An instance of that class is like a house built from that blueprint. Think about it. An infinite number of houses could be constructed using those blueprints. But the architect only had to draw them once. __init__() is like the foundation of each house that is built. In otherwords, the stuff you do first when construction commences. Alan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class to function
On 2014-05-25 09:20, R. Alan Monroe wrote: can you guys help explain. super thanks A class is like a blueprint. An instance of that class is like a house built from that blueprint. Think about it. An infinite number of houses could be constructed using those blueprints. But the architect only had to draw them once. __init__() is like the foundation of each house that is built. In otherwords, the stuff you do first when construction commences. Alan I very much like this analogy but might I suggest a slight modification? __init__() builds the house, and possibly provides minimum furnishings[1] (with parameters passed to init;) then your code (usually using the associated methods) flushes out the furnishings[1] (and makes use of them) as the needs of your program demand. [1] 'furnishings' in this analogy are DATA Does this fit? Comments welcome. Respectively submitted, ak ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class to function
On 25/05/14 16:39, rahmad akbar wrote: Hi guys i am trying to understand this code: http://nbviewer.ipython.org/gist/BenLangmead/6665861 i understand functions quite alright . but i have no idea about classes yet. Do you understand modules? Modules contain functions and data that you can reuse across programs. Are you comfortable with the idea of importing and using modules? Classes are one step on from modules. A module can contain data but that data is restricted in that you can only have a single instance of it. For example if you had a module full of functions for processing a data tree and the root was a module variable. You can load the tree and use it just fine. But if you want to work with more than one tree you either have to throw away (or cache) the old tree before populating the new one or combine both trees under a single root (which probably won't work well). To solve that you build the module functions and data into a Tree class. Then you can create multiple Trees each with their own root. To do that you take each of the module functions and put then into the class as methods. You then put the global root variable into the class as instance data. You do that by creating an __init__() method that accepts the root data as a parameter and populates the instance root variable (self.root). When you create an instance of a tree you pass in the root variable and Python magically calls the __init__() method, passing your root in to it. You probably have been doing this with things like strings for ages. You create a string by assigning a string literal s = 'my string' instead of explicitly calling s = str('my string') But the effect is the same. You create an instance of a string object which has methods attached, like upper(), split() etc, -- 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] class to function
i am trying to understand this code: http://nbviewer.ipython.org/gist/BenLangmead/6665861 I'm slightly familiar with the purpose of the code. It's constructing a Suffix Tree, though not in linear time. Reading the code... ah. I see. This is enumerating through all suffixes, and building it by repeated insertion into a trie. http://en.wikipedia.org/wiki/Trie The classes are used here to represent structured data and operations to be performed on that structured data. The code fundamentally has a structured value called a Node, with two fields to represent the string label and the links to other nodes. Aside: there is a very good book by Dan Gusfield that talks about suffix trees and how to construct them called Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology http://www.amazon.com/Algorithms-Strings-Trees-Sequences-Computational/dp/0521585198 which you may want to look at if you're interested in these algorithms. It is probably not a good idea to try to intermix trying to understand an algorithm like this at the same time as you're learning basic features in your programming language. Consider a slightly simpler example to learn about classes, apart from the algorithms you are studying. Most good Python tutorials should cover how to use classes to build structured data and manipulate it. Alan Gauld's Python tutorial, for example, should have a section on this. http://www.alan-g.me.uk/tutor/tutclass.htm ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor