Re: [Tutor] hi
On 20 August 2013 13:49, Vick vick1...@orange.mu wrote: From: Oscar Benjamin [mailto:oscar.j.benja...@gmail.com] Well just send me some tutorial on how to build and obtain the coefficients for the butcher tableau for the RK4 as an example, and after I've mastered it, I'd give the dopri8 a shot. I am up for it so I'll see if I can find time to write a script that shows how to do it. [Vick] Hope you've had the time to code it. I'm waiting for it. Sorry, I haven't found the time yet. It is still on my todo list though! By the way your code for the Adams-Moulton coefficients are actually the Adams-Bashforth ones and so I copied it and modified the copy to have the Adams-Moulton coefficients as well. This means that I have now an nth-order predictor-corrector method to solve for ODEs. Oh sorry. That'll be a cut and paste error. My code lives in a private software library that I keep meaning to release on PyPI but it's not ready for public consumption in quite a number of ways. I'm glad that you worked it out though. You''ll probably understand what I mean now when I say that the AM or AB integrators need a secondary algorithm to bootstrap. The accuracy of the subsequent AM/AB method depends on the accuracy of that step. In the worst case you can just use rk4 with a very small time-step for this bit though. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] global variables
I'm always told to avoid using them. I read discussions on the python irc channel about them but honestly i feel there are some times where i can't avoid using them. Like where i want to keep track of a state variable in many different functions that may or may not alter its value and also not wanting any of the functions to return it to the caller. My question is how many global variables did your last decent sized program have? Also please share any insight you have about them. I do try to avoid them, but is this always possible? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 2013-08-22 13:36, Matthew Ngaha wrote: I'm always told to avoid using them. I read discussions on the python irc channel about them but honestly i feel there are some times where i can't avoid using them. Like where i want to keep track of a state variable in many different functions that may or may not alter its value and also not wanting any of the functions to return it to the caller. It sounds like you want to use a class. My question is how many global variables did your last decent sized program have? Also please share any insight you have about them. I do try to avoid them, but is this always possible? I don't have any global variables in any of my projects, and I've been programming Python in some capacity for almost 8 years now. Why would you not just use a class if you want to store state? pgp8g3wmh7NcT.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On Thu, Aug 22, 2013 at 1:40 PM, Chris Down ch...@chrisdown.name wrote: It sounds like you want to use a class. Why would you not just use a class if you want to store state? I don't feel my program needs a class. Also i have been told to stop using classes by some very experienced Python programmers on irc even though i don't see why. It's confusing being told different things. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 2013-08-22 14:43, Matthew Ngaha wrote: I don't feel my program needs a class. Also i have been told to stop using classes by some very experienced Python programmers on irc even though i don't see why. It's confusing being told different things. Well, if you want to store state, you should really be using a class. What has made you think that your program doesn't need a class? There's no need, there's just what's best suited to your problem case (which you have not made clear, so nobody can comment on it). No experienced Python programmers are going to universally tell you not to use classes, likewise, no experienced Python programmers are going to universally tell you to use them all the time. It's a matter of context and suitability, which is entirely dependent on what it is that you are coding in the first place. I would doubt that anyone has told you don't ever use classes, because that's nonsense; you've probably misread a dissuasion from that path in a single instance as applying more broadly than was intended. pgpKOAYswETAY.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 22/08/13 13:36, Matthew Ngaha wrote: I'm always told to avoid using them. Global variables in themselves are not the problem. It's how they tend to get used that causes problems. Read-only global values - aka constants (so not really variables!) - are not an issue. Globals that are only changed via a set of dedicated functions are not topo much of a problem - although they should probably be bundled as a class (or module) if you have such features available. i can't avoid using them. Like where i want to keep track of a state variable in many different functions Usually that should be in a class. A class represents a set of operations using common data. Therefore shared state would naturally fit in a class along with the operations which depend on/modify that state. You may then have a single global variable which is the instance of that class. also not wanting any of the functions to return it to the caller. Thats more problematic and usually a sign of a bad design. Even if using global variables you should modify them explicitly via return values of functions rather than as hidden side-effects inside other functions. mystate = changestate(mystate, some, other, args) My question is how many global variables did your last decent sized program have? I usually have not more than a half dozen plus one per thousand lines of code. So in a 10,000 line program I'd expect to have less than 16 in total (actually I'd hope less than 10!). And many of those would be instances of classes. I would not expect to have more than one or two fundamental typed globals (ints, strings, bools etc), if any. Also please share any insight you have about them. I do try to avoid them, but is this always possible? It is possible but only by playing silly games with semantics such as: class MyProgram global1 = 0 global2 = True def __init__(self, lots, of, args, self.inst1 = # initialise program runtime vars def run(self) # my program code all goes here # and accesses the class level globals and instance # level runtime and state values if __name__ = __main__: MyProgram(args).run() [Or alternatively you can hide them inside a database.] Now technically there are no globals but in fact we are just moving them inside the meaningless class and have all the same potential issues with global side effects etc. In my experience there are usually a few globals required for any meaningful program. It's not avoiding globals completely that's important, it's being careful to use them sensibly and with good adherence to the principles of coupling and cohesion in the design. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 22/08/13 14:43, Matthew Ngaha wrote: On Thu, Aug 22, 2013 at 1:40 PM, Chris Down ch...@chrisdown.name wrote: It sounds like you want to use a class. Why would you not just use a class if you want to store state? Local coding conventions or programmer skill levels may preclude it. I don't feel my program needs a class. But in this case it sounds like a class is the best solution. Why would you feel that you don't need a class when you have a situation where several functions share common state? That's almost the definition of a class. Also i have been told to stop using classes by some very experienced Python programmers on irc Really? What reasons did they give. Unless they are talking about very specific circumstances that doesn't sound like good advice! It's confusing being told different things. Software engineering, like any branch of engineering, is about learning to use many different tools and selecting the best set for a problem. There are cases where classes are not in the best set, there are cases where many global variables are a good fit. But both of these are the exceptions to the rule and the engineer's job is to identify when a genuine exception exists and make the right choice. There is never a single right answer. Sorry, but that's life. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On Thu, Aug 22, 2013 at 2:52 PM, Chris Down ch...@chrisdown.name wrote: I would doubt that anyone has told you don't ever use classes, because that's nonsense; you've probably misread a dissuasion from that path in a single instance as applying more broadly than was intended. I am being totally honest here. I was very confused at the time and i said i didn't agree because it's what i had put so much effort into learning. They went on to say at some well known Python talks speakers have stated why using OOP (especially inheritance, but not excluding any others) is very bad design and the same thing can always be achieved without it. To be clear they said every use case OOP is the worst option. I asked what about GUIs which their design is strongly based around OOP? and they sad GUIs are badly designed to begin with so it proves the point about OOP. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On Thu, Aug 22, 2013 at 3:04 PM, Alan Gauld alan.ga...@btinternet.com wrote: On 22/08/13 13:36, Matthew Ngaha wrote: Global variables in themselves are not the problem. It's how they tend to get used that causes problems. Globals that are only changed via a set of dedicated functions are not topo much of a problem - although they should probably be bundled as a class (or module) Usually that should be in a class. A class represents a set of operations using common data. Therefore shared state would naturally fit in a class along with the operations which depend on/modify that state. Oh this makes a lot of sense. Sorry about disagreeing with Chris i now see that perhaps a class is definately the best choice for keeping a state variable. Thats more problematic and usually a sign of a bad design. Even if using global variables you should modify them explicitly via return values of functions rather than as hidden side-effects inside other functions. mystate = changestate(mystate, some, other, args) Yeah i have to agree about the bad design now i think about it. I didn't see the issues before reading this It is possible but only by playing silly games with semantics such as: class MyProgram global1 = 0 global2 = True [Or alternatively you can hide them inside a database.] Now technically there are no globals but in fact we are just moving them inside the meaningless class and have all the same potential issues with global side effects etc. About the class variables, i have used them a lot without realising they had all the same side effects as global variables. Something i now have to try a different approach with. In my experience there are usually a few globals required for any meaningful program. It's not avoiding globals completely that's important, it's being careful to use them sensibly and with good adherence to the principles of coupling and cohesion in the design. Thanks alot! This is definately a lot of food for choice and will help in my future decisions. I'm happy i asked this question as i can honestly say i had been developing some bad habbits. The responses have definately been helpful about program design and not just global variables. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 2013-08-22 15:12, Matthew Ngaha wrote: I am being totally honest here. I was very confused at the time and i said i didn't agree because it's what i had put so much effort into learning. They went on to say at some well known Python talks speakers have stated why using OOP (especially inheritance, but not excluding any others) is very bad design and the same thing can always be achieved without it. To be clear they said every use case OOP is the worst option. I asked what about GUIs which their design is strongly based around OOP? and they sad GUIs are badly designed to begin with so it proves the point about OOP. Were these expert Python programmers smoking crack cocaine at the time? pgpQLd06_zYdS.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On Thu, Aug 22, 2013 at 10:12 AM, Matthew Ngaha chigga...@gmail.com wrote: On Thu, Aug 22, 2013 at 2:52 PM, Chris Down ch...@chrisdown.name wrote: I would doubt that anyone has told you don't ever use classes, because that's nonsense; you've probably misread a dissuasion from that path in a single instance as applying more broadly than was intended. I am being totally honest here. I was very confused at the time and i said i didn't agree because it's what i had put so much effort into learning. They went on to say at some well known Python talks speakers have stated why using OOP (especially inheritance, but not excluding any others) is very bad design and the same thing can always be achieved without it. To be clear they said every use case OOP is the worst option. I asked what about GUIs which their design is strongly based around OOP? and they sad GUIs are badly designed to begin with so it proves the point about OOP. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Discussions among developers concerning the suitability of global variables have been going on at least since the 1970s. In my view they are the data equivalent of the 'goto' statement which has pretty much disappeared from acceptable coding practice. Think of them this way: Take a group of housemates who all use the same refrigerator. They all work different hours, spend time with different friends, and never talk to each other. When Bob buys beer, and later comes home to find there is no beer in the refrigerator it frustrates him. He doesn't even know who to complain to because anyone in the house could have taken it -- even a visitor. So, globals seem like a great idea to start with. But over time they cause trouble. I think you should write your code with globals if you think its best. Probably your code isn't that important, and it won't matter what you did in this program in a year or two. Anyway you will be learning. If your code is important for work, or some other purpose, over time it will be changed, and expanded, and problems will arise. Then you, or worse some one else, will have to figure out what is going wrong. That's where you will rue the day you sprinkled globals here and there. -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On Thu, Aug 22, 2013 at 10:12 AM, Matthew Ngaha chigga...@gmail.com wrote: On Thu, Aug 22, 2013 at 2:52 PM, Chris Down ch...@chrisdown.name wrote: I would doubt that anyone has told you don't ever use classes, because that's nonsense; you've probably misread a dissuasion from that path in a single instance as applying more broadly than was intended. I am being totally honest here. I was very confused at the time and i said i didn't agree because it's what i had put so much effort into learning. They went on to say at some well known Python talks speakers have stated why using OOP (especially inheritance, but not excluding any others) is very bad design and the same thing can always be achieved without it. To be clear they said every use case OOP is the worst option. I asked what about GUIs which their design is strongly based around OOP? and they sad GUIs are badly designed to begin with so it proves the point about OOP. OK, I'm not a fantastic programmer in any language, but this strikes me as someone with an axe to grind giving bad advice to new programmers. Here are a few of the things that I see wrong with their statements: 1) For almost every option in programming, there's at least one case where it's a good idea, design-wise. Saying in every use case OOP is the worst option is absurd. Of course there are cases where it's not the worst option. There are also cases where it is. That goes for just about everything. 2) If they think OOP is always a bad idea, WHY are they using Python? Isn't object orientation kind of the whole POINT of Python? From python.org: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. If they honestly believe that object oriented programming is always a bad idea, they really need to pick a different language. Perl, maybe, although even in Perl people are doing object oriented work. I have trouble believing that someone who believes OOP is inherently bad is a current high-level programmer in Python, unless they're trapped in a job they don't want to be doing. 3) Bad design in a product does not mean bad design in the tool used to build it. I've built some really terrible things out of wood with really nice tools. I've watched people use really nice drafting tools to design houses that would have been unusable for living in. Saying badly designed GUIs prove that OOP is bad is, frankly, illogical at best and stupid at worst. I strongly suspect that either the speaker they were listening to wasn't clear, or they weren't clear. Either that, or the speaker or whoever you were talking to mis-represented their ability level. Andy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 22/08/13 23:43, Matthew Ngaha wrote: On Thu, Aug 22, 2013 at 1:40 PM, Chris Down ch...@chrisdown.name wrote: It sounds like you want to use a class. Why would you not just use a class if you want to store state? I don't feel my program needs a class. Also i have been told to stop using classes by some very experienced Python programmers on irc even though i don't see why. It's confusing being told different things. If you're talking about #python on Freenode, in my experience, they are rude and unpleasant people with an inflated sense of their own intelligence. But they possibly think the same about me ;-) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 22/08/13 22:36, Matthew Ngaha wrote: I'm always told to avoid using them. I read discussions on the python irc channel about them but honestly i feel there are some times where i can't avoid using them. Like where i want to keep track of a state variable in many different functions that may or may not alter its value and also not wanting any of the functions to return it to the caller. My question is how many global variables did your last decent sized program have? Also please share any insight you have about them. I do try to avoid them, but is this always possible? Good question! And one that needs a long answer. Yes, it is possible to avoid global variables. The proof of this, if you need any, is programming languages like Haskell which are purely functional languages, which means they don't have variables at all! Everything is a fixed value, usually created on the fly as needed, and thrown away when no longer required, but never modified[1]. Need to change something? You create a new value rather than modify an existing one. Haskell programmers are capable of writing large programs, and they do it without global variables. Functional programming is a very different paradigm, and one which takes a while to get used to. Haskell, which is pure functional, can be hard to wrap your head around, but Python is strongly influenced by Haskell and Lisp and could be considered a semi-functional language. If you've ever used list comprehensions or generator expressions, you've got a small taste of functional programming. Truth be told, you *could* write a large, non-trivial Python program in entirely functional style, but without a clever Lisp or Haskell compiler behind it, it would probably be just as slow to run as it would be hard to write. In Python, you should consider global variables to be something to be minimized rather than entirely avoided. In my experience, global variables are well-suited to dealing with user preferences and command-line options, and otherwise best avoided. But notice that user prefs and cmd line options aren't exactly *variables*, since they are typically set once, when the program starts up, and then never -- or at least hardly ever -- changed. Your functions may read their value, but they will not usually change their value. That brings us to why global variables are unsafe. They're not unsafe because they are global. Global constants, or almost-constants, are perfectly fine. They're unsafe because they vary unpredictably. They introduce coupling between functions which otherwise should be independent. For example, suppose we have a global variable and three functions, f, g, h, all of which read and write to the global under various situations. Now suppose you call f() -- the result you get depends on whether or not g() or h() have been called first, since they may change the global. And likewise, the result g() gives depends on whether or not f() or h() have been called, and similarly for h(). So instead of the result of f() depending in a nice clean way only on the arguments passed to it, instead it depends in a messy, convoluted, confusing way on the history of calls to separate functions that may have nothing to do with f(). You now have a tangle of couplings between functions: f depends on g and h g depends on f and h h depends on f and g Now imagine ten globals and twenty functions. Globals introduce action at a distance. A function in one part of your program can reach out over a great distance and effect another function's result, just by changing a global. That's a bad thing and should be minimized or eliminated. So the real problem here is *state*. If your functions didn't depend on mutable state, but only on arguments passed to them, the problem of action at a distance would go away. This is where functional programming comes in: they eliminate mutable state altogether. Once a value is created, you can't change it, only throw it away and create a new one. In Python we don't necessarily go quite to that extreme, but it's still a good ideal to work towards. One mistake people have is to create a class just to hold global variables, and then think that they are virtuous because it's not a global. So then they end up with something like this: class Everything_I_Need: def __init__(self): self.a = 1 self.b = 2 self.c = 3 # etc. everything = Everything_I_Need() f(everything) g(everything) h(everything) but of course this is just global variables in disguise, and still suffers from the same problem with strong coupling and action at a distance. [1] Never say never. Haskell actually does have a way to create modifiable values, but to get an idea of how people consider it, it is often called the unsafePerformIO hack. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options:
Re: [Tutor] global variables
On 22/08/13 22:36, Matthew Ngaha wrote: My question is how many global variables did your last decent sized program have? Also please share any insight you have about them. I do try to avoid them, but is this always possible? Eiffel is another language that aims to eliminate global variables entirely. Here is an excellent article by the creator of Eiffel explaining why and how: http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/globals.html -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 22/08/13 23:52, Chris Down wrote: On 2013-08-22 14:43, Matthew Ngaha wrote: I don't feel my program needs a class. Also i have been told to stop using classes by some very experienced Python programmers on irc even though i don't see why. It's confusing being told different things. Well, if you want to store state, you should really be using a class. Depends on how much state and what you do with it. But fundamentally, if you want to store state, you should try very, very hard to *avoid* storing state unless you really need to. Or at least, make sure that there is a very strict demarcation between functions (or methods) which change that state, and those which do not. The ultimate aim is to avoid the bad parts of storing state, without necessarily going all the way to strict functional code: - reduce or eliminate side-effects wherever possible; - functions that have side-effects should do so only in well-understood and innocuous ways; - eliminate coupling between unrelated parts of code; - reduce coupling within related parts of code; - avoid action at a distance whenever possible. If you keep these aims in mind, the problem of global variables will solve itself. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 23/08/13 00:12, Matthew Ngaha wrote: On Thu, Aug 22, 2013 at 2:52 PM, Chris Down ch...@chrisdown.name wrote: I would doubt that anyone has told you don't ever use classes, because that's nonsense; you've probably misread a dissuasion from that path in a single instance as applying more broadly than was intended. I am being totally honest here. I was very confused at the time and i said i didn't agree because it's what i had put so much effort into learning. They went on to say at some well known Python talks speakers have stated why using OOP (especially inheritance, but not excluding any others) is very bad design and the same thing can always be achieved without it. To be clear they said every use case OOP is the worst option. I asked what about GUIs which their design is strongly based around OOP? and they sad GUIs are badly designed to begin with so it proves the point about OOP. Well I agree with that last point. As far as avoid OOP, I suspect they were thinking about this (in)famous video: http://pyvideo.org/video/880/stop-writing-classes Here is a counter-viewpoint: http://lucumr.pocoo.org/2013/2/13/moar-classes/ I think both have excellent advice. Many people do write unnecessary, complicated classes, and many people do unnecessarily lock up the inner workings of their code. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On 23/08/13 00:59, Andy McKenzie wrote: Isn't object orientation kind of the whole POINT of Python? From python.org: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Well, yes and no. Python is an object-oriented language in the sense that all of Python is built using objects. Everything, including modules, functions, ints, strings, yes, even classes themselves, are objects. But also no, in the sense that the code you write doesn't have to be written using OOP techniques. Python is a multi-paradigm language in the sense that you can write code using any of these styles: - object-oriented - functional - procedural - imperative - or a mix of all of the above. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
On Thu, Aug 22, 2013 at 4:35 PM, Steven D'Aprano st...@pearwood.info wrote: Good question! And one that needs a long answer. Hey i just checked mail again... A big thank you for your responses, i will read all of them now.. im pretty my programs/designs will be much cleaner by the time i done. And yes i was talking about freenode:) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How much in a try block?
If I have a series of tasks that depend on X happening, should I put them all in the same try block or just put X in there and exit out if it fails? Thanks! Leam -- Mind on a Mission http://leamhall.blogspot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How much in a try block?
On Fri, Aug 23, 2013 at 6:14 AM, leam hall leamh...@gmail.com wrote: If I have a series of tasks that depend on X happening, should I put them all in the same try block or just put X in there and exit out if it fails? You are right about the latter. You should put only the statement which you expect to raise the exception. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How much in a try block?
On 2013-08-23 06:20, Amit Saha wrote: On Fri, Aug 23, 2013 at 6:14 AM, leam hall leamh...@gmail.com wrote: If I have a series of tasks that depend on X happening, should I put them all in the same try block or just put X in there and exit out if it fails? You are right about the latter. You should put only the statement which you expect to raise the exception. You can also use the else clause if there is stuff you want to run if the try block doesn't raise the caught exception, which avoids putting it in try if you don't intend to exit from the exception. pgp3aZGkPDAT6.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables
Steve, Thanks for all of this information. It seems the OP has sparked the kind of discussion that I had hopped for when I posted http://thread.gmane.org/gmane.comp.python.tutor/83251 Luckily though I did get an excellent response from Alan. http://thread.gmane.org/gmane.comp.python.tutor/83251/focus=83252 Still what you have posted here has been a good read, so, thank you. -- Jordan On Thu, Aug 22, 2013 at 12:18 PM, Steven D'Aprano st...@pearwood.info wrote: On 23/08/13 00:59, Andy McKenzie wrote: Isn't object orientation kind of the whole POINT of Python? From python.org: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Well, yes and no. Python is an object-oriented language in the sense that all of Python is built using objects. Everything, including modules, functions, ints, strings, yes, even classes themselves, are objects. But also no, in the sense that the code you write doesn't have to be written using OOP techniques. Python is a multi-paradigm language in the sense that you can write code using any of these styles: - object-oriented - functional - procedural - imperative - or a mix of all of the above. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How much in a try block?
On 22/08/13 21:27, Chris Down wrote: You can also use the else clause if there is stuff you want to run if the try block doesn't raise the caught exception, which avoids putting it in try if you don't intend to exit from the exception. I admit that I've never really found a use for else in a try block. I don;t see much advantage in try: f(x) except MyError: pass else: g(x) h(x) over try: f(x) except MyError: pass g(x) h(x) Unless you really only want g(x) executed if there is no MyError exception but want h(x) executed regardless. I guess where h() is not using x it might be helpful but in most(all?) of my code I've usually bailed when x has gone wrong or I've fixed things such that hg() and h() are required. I'm curious, how often do others use the try/else combination? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How much in a try block?
On Fri, Aug 23, 2013 at 10:30 AM, Alan Gauld alan.ga...@btinternet.com wrote: On 22/08/13 21:27, Chris Down wrote: You can also use the else clause if there is stuff you want to run if the try block doesn't raise the caught exception, which avoids putting it in try if you don't intend to exit from the exception. I admit that I've never really found a use for else in a try block. I don;t see much advantage in try: f(x) except MyError: pass else: g(x) h(x) over try: f(x) except MyError: pass g(x) h(x) Unless you really only want g(x) executed if there is no MyError exception but want h(x) executed regardless. I guess where h() is not using x it might be helpful but in most(all?) of my code I've usually bailed when x has gone wrong or I've fixed things such that hg() and h() are required. I'm curious, how often do others use the try/else combination? I think one use of writing something in an else would be to write those statements which are part of a block (loosely used) of statements which would depend on the statement in the try block. That doesn't change the way it works if they were not written in an else, but perhaps makes it clear that those statements are to be executed only when there is no exception. Explicit is better than implicit, may be? -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How much in a try block?
Depending on what x is and also whether or not you've decided to go the 'try'route, if, else may be useful from the way you sound. My thought. On 22 Aug 2013 21:16, leam hall leamh...@gmail.com wrote: If I have a series of tasks that depend on X happening, should I put them all in the same try block or just put X in there and exit out if it fails? Thanks! Leam -- Mind on a Mission http://leamhall.blogspot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How much in a try block?
On 23/08/13 10:30, Alan Gauld wrote: I admit that I've never really found a use for else in a try block. I don;t see much advantage in try: f(x) except MyError: pass else: g(x) h(x) over try: f(x) except MyError: pass g(x) h(x) Unless you really only want g(x) executed if there is no MyError exception but want h(x) executed regardless. That's exactly the use-case for try...else. Admittedly, it's a relatively uncommon use-case, but when you need it, you need it. Instead of writing something like this: failed = False try: do_stuff() except SomeException: failed = True handle_error() if not failed: do_something_else() always_do_this() a cleaner design is to use try...else: try: do_stuff() except SomeException: handle_error() else: do_something_else() always_do_this() The advantage of try...else is even more obvious when you combine it with a finally clause: try: do_stuff() except SomeException: handle_error() else: do_something_else() finally: cleanup() always_do_this() The cleanup function runs no matter how you exit the try block, whether it is via the except clause, the else clause, or even an unhandled exception. In this case, do_something_else is supposed to run before cleanup. I might be tempted to write this instead: failed = False try: do_stuff() except SomeException: failed = True handle_error() finally: cleanup() if not failed: do_something_else() always_do_this() but that's wrong, because now do_something_else runs after cleanup. So I would have to use two try blocks to match the behaviour of try...else...finally. try: failed = False try: do_stuff() except SomeException: failed = True handle_error() if not failed: do_something_else() finally: cleanup() always_do_this() I'm curious, how often do others use the try/else combination? Rarely :-) In my statistics module[1], out of nearly 2300 lines of code including doc strings, I use else just ten times in total and none of them are from try...else. On the other hand, in my startup file that runs when I launch the Python interactive interpreter, a mere 157 lines, I have *four* try...else blocks. So it really depends on the nature of the code. Here is a typical example: # === Command line completion and history === try: import history except ImportError: print('*** warning: command line history not available ***') else: history = history.History() [1] http://www.python.org/dev/peps/pep-0450/ -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to present python experience (self-taught) to potential employer
Hi everyone, This is Jing and I am a recent college graduate with Biology and Public Health background. I'm currently learning python on my own when i have time off from my PH internship. There's a job posting that looks really idea for me in the near future (a PH Research position) that requires Python experience and I wonder if any of you have any suggestions how I can demonstrate my python skills if I'm learning it on my own as opposed to taking courses? Some people had previously suggested GitHub, but it seems to only show my abilities to read python code and detect bugs, but not abilities to write python code. Some others suggested doing a project of my own, but I don't currently have any data or problem to solve in my field. Thanks so much! Jing ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python tutoring
Hi Trent, I was once wondering about the similar question, but I discovered that Python has a huge user group (stationed across the world) and here's a site that includes the local links to all the user groups around the world, South Korea is included! You can probably find help locally from there. http://wiki.python.org/moin/LocalUserGroups#Korea On Tue, Aug 20, 2013 at 9:22 AM, Fowler, Trent fowle...@hendrix.edu wrote: Hello, Not long ago I came across the website of a professional programmer offering python tutoring services: http://www.jeffknupp.com/python-tutoring/ I have attempted to contact him because I am interested but I've been unable to get in touch. I was wondering if anyone knew of people offering similar services. I am a self-starter and highly motivated, but I live in a small town in South Korea and I don't have any friends who program. Since I also don't have a computer science background and python is my first language, I really need someone who can help me with the beginning stages. Often times when I run into a problem not only do I not know how to solve it, I don't even know how to ask the questions that will help someone else solve it. I don't want to be spoon-fed, just gently nudged and guided. I'm on a budget but I'd be willing to pay for a good teacher. Preliminary googling has turned up precious little, so I thought someone here might be able to point me in the right direction. Thanks, -Trent. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to present python experience (self-taught) to potential employer
Hi Jing Ai, On Fri, Aug 23, 2013 at 12:45 PM, Jing Ai jai...@g.rwu.edu wrote: Hi everyone, This is Jing and I am a recent college graduate with Biology and Public Health background. I'm currently learning python on my own when i have time off from my PH internship. There's a job posting that looks really idea for me in the near future (a PH Research position) that requires Python experience and I wonder if any of you have any suggestions how I can demonstrate my python skills if I'm learning it on my own as opposed to taking courses? Some people had previously suggested GitHub, but it seems to only show my abilities to read python code and detect bugs, but not abilities to write python code. Some others suggested doing a project of my own, but I don't currently have any data or problem to solve in my field. I am not from your background. In your field of work do you need to do lot of data analysis (read statistical analysis)?. Do you think you could find something like that and something of your interest? You could then use Python (the language and related tools) to perform data analysis, presenting data graphically, etc to work the data and infer some relevant conclusions. For example, http://www.quandl.com/ has data sets related to various fields of study. Does that sound like something you may find interesting and is relevant? Best, Amit. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to present python experience (self-taught) to potential employer
On Fri, Aug 23, 2013 at 1:52 PM, Jing Ai jai...@g.rwu.edu wrote: @Amit Thank you for your suggestions! I'll look into the data there and see if there's something relevant that I can use to do a project. Yes I believe it would involve some data analysis (and I may need to learn R as well or use RPy). Do you think one project is sufficient to demonstrate my skills if it's in-depth? Or does it take several projects? Hmm I am not sure. But, depends on how much time you have. If you can do one big project that demonstrates a number of your skills - use of Python and one or more of the scientific libraries, that perhaps speaks fair bit about what you know. Also, consider using version control for your projects and of course, unit testing. I also suggest looking into Sphinx for documentation of your project. They also demonstrate that you know some of the things that you need to beyond just writing programs. Best of luck. -Amit. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to present python experience (self-taught) to potential employer
@Amit Thank you for your suggestions! I'll look into the data there and see if there's something relevant that I can use to do a project. Yes I believe it would involve some data analysis (and I may need to learn R as well or use RPy). Do you think one project is sufficient to demonstrate my skills if it's in-depth? Or does it take several projects? @Japhy Thanks for your response. I don't really mean that I can't think of any problems in my fields of study, but rather that I'm inexperienced in selecting a specific problem that's appropriate (not too simple or complex) to work with based on my current Python experience. I agree that python skills are demonstrated through writing though. On Thu, Aug 22, 2013 at 11:12 PM, Amit Saha amitsaha...@gmail.com wrote: Hi Jing Ai, On Fri, Aug 23, 2013 at 12:45 PM, Jing Ai jai...@g.rwu.edu wrote: Hi everyone, This is Jing and I am a recent college graduate with Biology and Public Health background. I'm currently learning python on my own when i have time off from my PH internship. There's a job posting that looks really idea for me in the near future (a PH Research position) that requires Python experience and I wonder if any of you have any suggestions how I can demonstrate my python skills if I'm learning it on my own as opposed to taking courses? Some people had previously suggested GitHub, but it seems to only show my abilities to read python code and detect bugs, but not abilities to write python code. Some others suggested doing a project of my own, but I don't currently have any data or problem to solve in my field. I am not from your background. In your field of work do you need to do lot of data analysis (read statistical analysis)?. Do you think you could find something like that and something of your interest? You could then use Python (the language and related tools) to perform data analysis, presenting data graphically, etc to work the data and infer some relevant conclusions. For example, http://www.quandl.com/ has data sets related to various fields of study. Does that sound like something you may find interesting and is relevant? Best, Amit. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor