Re: [Tutor] advice on global variables
On Tue, Jul 10, 2012 at 12:11 PM, Chris Hare wrote: > > I know they are bad. That is why I would prefer not to use it, but I am not > sure how else to handle this problem. > > In this app, the user must log in. Once authenticated, they have a userid > stored in the SQLite database. Before splitting my app into multiple files, > I used a global variable. I know its bad, but it worked. Now that things > are split apart, the classes which used it previously now don't see it > anymore, even using the global keyword. I think this is the expected > behavior. See here > > file: a.py > > import b > global_var = "global" > > def func1(): > global global_var > print "global var in func1 = %s" % global_var > > class intclass: > def func2(self): > global global_var > print "global var in intclass = %s" % global_var > > print "global_var = %s" % global_var > func1() > f = intclass() > f.func2() > g = b.extclass() > g.func3() > > file: b.py > > class extclass: > def func3(self): > global global_var > print "global var in extclass = %s" % global_var > > When I run it, I get what I think the expected behavior, that the external > class ext class won't be able to see the global_var > > Big-Mac:t chare$ python a.py > global_var = global > global var in func1 = global > global var in intclass = global > Traceback (most recent call last): > File "a.py", line 18, in > g.func3() > File "/Users/chare/Development/python/animaltrax/pkg/t/b.py", line 5, in > func3 > print "global var in extclass = %s" % global_var > NameError: global name 'global_var' is not defined > > So - my question is this: how do I solve the problem of multiple classes > needing to get access to a value which needs to be preserved across the > lifetime of the running application? > > One thought was a RAM based SQLite database, but that seems like a lot of > work. I dunno, maybe that is the option. > > suggestions, ideas, criticisms are all welcome. Python code aside, I just > don't know how to approach this problem in Python. > > Thanks, as always for the feedback and guidance. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Can you tell us a little more about what this app does? As a general rule, if you're using a global variable it's a strong sign that you're doing something wrong. Can we see real code? To answer your most basic question: import a a.global_var But again, that's probably not the right way to solve your problem. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advice on global variables
On Tue, Jul 10, 2012 at 1:32 PM, Prasad, Ramit wrote: >> You should avoid using the global statement. >> >> In your case, I would think you could just add an argument to the method: >> >> class MyObj(object): >> def __init__(self, arg): >> self.arg = arg >> def my_func(self, new_arg): >> self.arg = new_arg >> >> to call it: >> >> arg = 1 >> >> m = MyObj(arg) >> print m.arg >> new_arg = 2 >> m.my_func(new_arg) >> print m.arg > > Just as a note, this would not really work if the variable needs to be > changed and read from several places when the value is an immutable > type such as numbers / strings. In that case, then you could use > the same logic but instead place the value in a list and pass that > and always check/update the first element of the list. > That's a terrible idea. That's basically replicating the behavior of a global while hiding that fact which makes the code even less readable. > > Ramit > > > Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology > 712 Main Street | Houston, TX 77002 > work phone: 713 - 216 - 5423 > > -- > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > ___ > 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] Define Build Deployment
On Wed, Jul 11, 2012 at 8:31 AM, James Bell wrote: > I'm fairly new to software development in an enterprise environment I'm > constantly hearing the term "build deployment" and do no want to ask what it > means since it seems simple. I cannot find a definition online. > > if the word is context specific please describe what it means at your > company. > > thanks > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > That's not python-related and the exact meaning will vary a lot organization to organization. My advice is to learn to ask stupid questions. That's the way we all learn. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does this tiny script works?
On Sat, Jul 14, 2012 at 8:32 PM, Santosh Kumar wrote: > I am reading How to Think Like a Computer Scientist with Python. There > is a script in the exercise: > > if "Ni!": > print 'We are the Knights who say, "Ni!"' > else: > print "Stop it! No more of this!" > > if 0: > print "And now for something completely different.." > else: > print "Whats all this, then?" > > > The question asks "Explain what happened and why it happened." I my > self is confused because the output is: > > We are the Knights who say, "Ni!" > Whats all this, then? > > 1. I was assuming that in first "if" statement we will get "Stop it! > No more of this!" because we are no setting any input and matching > string from it (in this case "Ni!"). > 2. I didn't get 2nd part at all. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Long story short, it has to do with how things get translated to booleans. An empty string, 0, None or an empty collection will be translated to False. Almost everything else is translated to True. So in the first case, python "translates" if "Ni!" to if True. In the second case, python "translates" if 0 to if False. You can see this if you pull up an interpreter and type >>> bool("Ni!") True >>> bool(0) False ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie Questions
On Mon, Jul 16, 2012 at 5:09 AM, Matthew Ngaha wrote: > Hi all. I'm new to Python and Programming in general. I've started out with > Python for beginners, and so far so good. My friend who i might add, is not > a programmer but has had experience in the world of programming (i dont know > how much but he claims a lot), has told me to forget about Python and focus > on PHP. He knows i studied a HTML and CSS course and told me for the > direction i'm going in, Python is not needed and won't give me all the > options or variety PHP can. Thats he's opinion, i'd rather make my own mind > up, but its lead me to these questions out of curiousity: > > a) at this early stage i dont exactly know what web options are:( but is > Python limited when it comes to matters relating to Web options/developing? PHP will get you from 0 to website by the first page of your first tutorial. That's very attractive if you're interested in web programming. Most likely, if you use Python, you'll learn how to use the language more generally, before learning how to apply the language to a website. However, in my experience (I learned PHP a long time ago and Python a few years ago) Python gives you the most reward. I've used Python for website building, but also lots of other useful applications. > b) Are there better options, or can Python get the job done as good as any? It depends upon the job. If you plan on work as a programmer/software engineer, you will need to learn many languages. Yes, hammers can be used to put in screws and you could probably figure out how to use a screwdriver to put in a nail, but really, you're going to be successful if you have both a hammer and a screwdriver in your toolbox. Java, C, Python, C++, each have their own uses. However, (and this may be a biased assessment) PHP is more like a hammer with its head removed and a screwdriver duct-taped on it. Sure, you can use it, but it's not going to be a pleasant experience. The languages which I would say are closest to Python in terms of where they are put to use are Perl and Ruby. My advice is this: give them each a shot (the first couple pages of a tutorial shouldn't take more than a few hours) and see which makes you feel the most comfortable. > c) after completing and understanding a beginner's book, would i be at > intermediate level, or still below? > d) Would i need a more advanced tutorial, what do you advise after finishing > a beginners course? Code, code, code. Programming is a practice. You'll learn the most by doing and researching solutions for specific problems you are encountering. Then, read blog posts, watch PyCon videos, look at the mailing list etc... Just expose yourself to the language and community and learn through osmosis. > > e) And finally, are there other essential things i must learn after Python? > i heard Django is important? Django is just one way to do Python web development. It's hugely useful for some things and terribly useless for others. There are plenty of other ways to do web development. > f) is Django the equivelent to PHP's MySql? As plenty of people have said, Django is a way to make websites while MySQL is a database system. You can actually use MySQL with Django. > > You dont have to answer all questions. Just answering one would help me > greatly in my future decisions, as i want to commit fully to the right > programming language. > > since this isnt a forum, how can i thank everyone for helping? > > > > ___ > 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] Why isn't my simple if elif script not working?
On Tue, Jul 17, 2012 at 10:09 PM, Santosh Kumar wrote: > Here is my script: > > name = raw_input("What's your name? ") > > if name == "Santosh": > print "Hey!! I have the same name." > elif name == "John Cleese" or "Michael Palin": > print "I have no preference about your name. Really!!" > else: > print "You have a nice name." > > > The if part works well. The elif part works well too. The problem is > even if you enter strings other than "Santosh", "John Cleese" and > "Michael Palin" you still get the print from elif part, not from else > part. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Think of it this way: The or operator doesn't look at both sides at the same time. It looks at one side, and then at the other. So on one side it looks and sees (name == "John Cleese") and decides whether that is true or not. Then it looks at the other side and sees ("Michael Palin") which is always true because a non-empty string is always true. It doesn't see "Michael Palin" in the context of name == "John Cleese". It sees and treats "Michael Palin" independently. So because "Michael Palin" is always True, the elif clause is always true. What you want to write is this: elif name == "John Cleese" or name == "Michael Palin": Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why isn't my simple if elif script not working?
On Tue, Jul 17, 2012 at 10:21 PM, Andre' Walker-Loud wrote: > Hi Santosh, > > On Jul 17, 2012, at 10:09 PM, Santosh Kumar wrote: > >> Here is my script: >> >> name = raw_input("What's your name? ") >> >> if name == "Santosh": >>print "Hey!! I have the same name." >> elif name == "John Cleese" or "Michael Palin": >>print "I have no preference about your name. Really!!" >> else: >>print "You have a nice name." >> >> >> The if part works well. The elif part works well too. The problem is >> even if you enter strings other than "Santosh", "John Cleese" and >> "Michael Palin" you still get the print from elif part, not from else >> part. > > you just have to be careful with the multiple boolean line in the elif. You > can use either > > elif name == ("John Cleese" or "Michael Palin"): That won't work. >>> "John Cleese" == ("John Cleese" or "Michael Palin") True >>> "Michael Palin" == ("John Cleese" or "Michael Palin") False >>> ("John Cleese" or "Michael Palin") 'John Cleese' Python will look at the expression ("John Cleese" or "Michael Palin") since bool("John Cleese") is True, the expression immediately evaluates to "John Cleese" and the elif clause becomes equivalent to name == "John Cleese" > > or > > elif name == "John Cleese" or name == "Michael Palin": > > > With your elif line, it is asking "does name == John Cleese" or "Michael > Palin", and so if the name is not John Cleese, then I believe it prints > "Michael Palin" while reporting True, so satisfying the elif clause. > > > Cheers, > > Andre > ___ > 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] Why isn't my simple if elif script not working?
On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano wrote: > Alexandre Zani wrote: > >> What you want to write is this: >> >> elif name == "John Cleese" or name == "Michael Palin": > > > elif name in ("John Cleese", "Michael Palin"): > > is better. > > > -- > Steven > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Better how? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why isn't my simple if elif script not working?
On Wed, Jul 18, 2012 at 7:44 AM, Steven D'Aprano wrote: > Alexandre Zani wrote: >> >> On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano >> wrote: >>> >>> Alexandre Zani wrote: >>> >>>> What you want to write is this: >>>> >>>> elif name == "John Cleese" or name == "Michael Palin": >>> >>> >>> elif name in ("John Cleese", "Michael Palin"): >>> >>> is better. > > >> Better how? > > > > It's shorter, you don't have to repeat the reference to `name` twice, it is > more easily extensible if you add additional names, and it is likely to be a > *tiny* bit faster -- it moves the equality test out of pure-Python code into > a tuple method, which will be written in C. > Wadayano... You're right, it is faster! Thanks. > > > > -- > 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