[Tutor] Query - Where to put in global variables, if needed, as a good programming practice
Hello, The point of good-bad-ness of global variables aside, if I needed to use them, which is a better place to put them. 1. In the __init__ function of a class? So they are available at the time an object is initialized or 2. In the actual function of the class where the variables are needed? Pros and Cons of either approach? Thanks and Regards, Sumod -- http://spawgi.wordpress.com We can do it and do it better. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice
On 15/06/2012 12:44, spa...@gmail.com wrote: Hello, The point of good-bad-ness of global variables aside, if I needed to use them, which is a better place to put them. 1. In the __init__ function of a class? So they are available at the time an object is initialized or 2. In the actual function of the class where the variables are needed? Pros and Cons of either approach? Thanks and Regards, Sumod ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor I don't understand why you would need global variables, and then promptly start discussing them wrt classes. Please explain what you are trying to achieve and I'm certain that we'll come up with the best solution for your use case. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice
Hi Spawgi, On 15 June 2012 12:44, wrote: > Hello, > > The point of good-bad-ness of global variables aside, if I needed to use > them, which is a better place to put them. > 1. In the __init__ function of a class? So they are available at the time an > object is initialized or Firstly note as a bit of an aside that member variables of an object (or perhaps of the class), are not considered global, at least not in the sense most people mean when they talk about "global" variables. I'm however assuming you're NOT referring to a variable that's actually either a class or object member variable in your question, but rather something akin to a true global. I say "akin", because Python does not really have true global variables in the sense that one might expect coming from some other langauges. The closest is probably the contents of the '__builtin__' module, which is the last searched when resolving names and hence practically speaking probably the closest to containing what one might describe as global variables, but I'd hazard to submit there will virtually without exception always be a better location for your variables than to plug them into the __builtin__ module. > 2. In the actual function of the class where the variables are needed? > Pros and Cons of either approach? OK, to state the obvious again, but if a variable is only needed in one method (function) of a class, then by definition you don't actually need a global. If it's needed in more than one method in an object, then you should probably consider an object member variable or suitably parameterizethe methods. That aside, to answer your question in a general fashion, initialisation should always happen in the initialization section (as appropriate) of the namespace that a variable is being declared/contained in. So package level varibles should be initialized in __init__.py of the package, module variables should be initialized in the module itself, prior to any other code that may refer to them obviously, and presumably towards the top of the module in most cases, class level variables should be initialized directly in the class prior to use, object level variables in the object initializer (__init__ method) and so on. So, your (presumably module) global variables should therefore *not* be initialized as part of the object initializer (what happens when you create multiple instances of your object?), object methods (what happens when you call the method multiple times?) or even plain functions (again, what happens when you call the method multiple times?) as you suggested, and assuming you're talking about module globals, they should in fact simply be initialized in the module, prior to use elsewhere in the module. (Use from other modules are possible by importing the module containing the module global variable from such other modules etc.) Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice
spa...@gmail.com wrote: Hello, The point of good-bad-ness of global variables aside, if I needed to use them, which is a better place to put them. 1. In the __init__ function of a class? So they are available at the time an object is initialized or 2. In the actual function of the class where the variables are needed? Pros and Cons of either approach? Neither of those are *global* variables. In Python, global variables are those at the top level of the module, and are only global to a single module, not your entire program. If your program is a single module, there is no difference. This confusion is one of the reasons that I hate the Java-ism of calling things-attached-to-classes-or-attributes as "variables" instead of members or attributes. In Python, the usual term for them is "attributes", and you can have class attributes shared between all instances of a class, and instance attributes that are specific to the instance. class K: shared = "this attribute is shared" def __init__(self): self.attribute = "this one is specific to the instance" Pros for class attributes: + they are shared, so all your instances see the same value + you can reach them directly from the class, without creating an instance first: K.shared works Cons for class attributes: - they are shared, so all your instances see the same value Pros for instance attributes: + they are not shared, so all your instances don't see the same value Cons for class attributes: - they are not shared, so all your instances don't see the same value - every time you create an instance, the attribute has to be created - you can't reach them directly from the class without creating an instance first: K.attribute does not work -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How does slicing work?
Hello,I'm a beginner in python.I found some qustions in Chapter 9's Quiz of the book* Learning Python,4th ed*, >>> L = [1, 2, 3, 4] >>> L[-1000:100] [1, 2, 3, 4] >>> L[3:1] [] >>> L[3:1] = ['?'] >>> L [1, 2, 3, '?', 4] How does python get the result of slicing?Such as the examples above,they really confused me. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does slicing work?
On 6/15/2012 7:48 PM Kanone Seele said... Hello,I'm a beginner in python.I found some qustions in Chapter 9's Quiz of the book/Learning Python,4th ed/, >> L = [1, 2, 3, 4] >> L[-1000:100] [1, 2, 3, 4] >> L[3:1] [] >> L[3:1] = ['?'] >> L [1, 2, 3, '?', 4] How does python get the result of slicing?Such as the examples above,they really confused me. There's some good examples at: http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation HTH, Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] getpass
Hey all, I'm working on a bit of code here and I'm having an issue with getpass. In the documentation, it says it hides the text entered "Prompt the user for a password without echoing." -- http://www.python.org/doc//current/library/getpass.html However when I run it in Eclipse, the password is clear as day. Here is a snippet of the code that I am working with. Any assistance would be greatly appreciated. = ... print 'Connected to Gmail' + '\n' try: gmail_user = str(raw_input('Enter your email: ')).lower().strip() gmail_pwd = getpass.getpass('Enter your email password: ').strip() smtpserver.login(gmail_user, gmail_pwd) -- ~MEN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] getpass
Mike Nickey wrote: Hey all, I'm working on a bit of code here and I'm having an issue with getpass. In the documentation, it says it hides the text entered "Prompt the user for a password without echoing." -- http://www.python.org/doc//current/library/getpass.html However when I run it in Eclipse, the password is clear as day. Then don't run it in Eclipse. Seriously. Hiding the user's input requires getpass to work hand-in-hand with the terminal. If another piece of software, like Eclipse, gets in the way, or if the terminal is not one which is supported, then getpass falls back on whatever it can, which unfortunately displays the password. Idle also fails to work properly with getpass. If you run getpass in Idle for Python 2.7, the prompt is written back to the parent terminal, not the Idle window, where the user may not see it. In general, code which requires intimate knowledge of the terminal should be run directly from a terminal and not via an intermediate layer like Eclipse or Idle. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor