Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    what does this stuff mean?

    New to Python and coding in general...
    I've been following the google app engine getting started tutorial and video and saw some things I'm not sure about....

    First of all, the dude typed this in at the top (first line)...
    Code:
    #!/usr/bin/env/python
    It doesn't seem to be necessary (everything works w/o it), and I've read that it has something to do w/ grabbing the python interpreter no matter where it is on someones system in Linux/BSD OS's... or something. Is it just habit on his part?... or is it good for the rest of us to do this for when our apps get put up on the google system?? could it hurt... cause errors?

    Next are these things...
    Code:
    %s  '/'  r'.*'
    used like this:
    Code:
    self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
    self.response.out.write('<blockquote>%s</blockquote>' %
    and...
    Code:
    self.redirect('/')
    [('/', MainPage),('/sign', Guestbook)]
    this:
    Code:
    [(r'.*',MyHandler)]
    And last, this if statement at the bottom also doesn't seem to have any effect on the code... the main function executes w/o it... so why put it? is it good practice?

    Code:
    if __name__ == "__main__":
        main()
    Still trying to get my head around this stuff and if anyone can help clear up my confusion it'd be appreciated!

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok it's been about 2 weeks and no replies, so I guess nobody else knows what this stuff means either. Also, I've noticed that as I search around for python related stuff, the more it really seems to be a niche language (on the web)... its growing fast I guess, but still has a very limited userbase and its hard to find tutorials & other info when compared to something like PHP (but I guess that's true for most languages).

    Here's what I've been able to find out:



    I've seen from a couple of sources now that...

    #!/usr/bin/env/python

    ...is in fact just for use on a Unix based system. It automatically tracks down the python interpreter wherever it is filed away, making your scripts more portable when jumping from machine to machine. So unless you use Linux (or some other Unix OS) and expect your code to be jumping around to other unfamiliar Unix OS computers, then you can just leave this out of your programs.



    These special characters:

    %s '/' r'.*'

    ...I'm still not sure about. So far, it looks like the %s is some kind of substitution character for strings in python.

    For example: "The %s went up the tree" % cat

    the '/' looks like its just a way for the main page in that path to refer to itself, but really not sure.

    and I think the r'.*' means: r = read-only, . = this type, * = all
    So in this case its "all files" and specifies read-only access (but, again, I'm not sure about this).



    And last is this:

    if __name__ == "__main__":
    main () (or whatever executes your script in place of "main()")

    ...which looks like it has to do w/ modules. As I've read more about python, modules look to be an important and powerful way to, well, modularize your code and keep from having to rewrite the same code over and over, or even from having to include all of a program's code, all in one huge file.

    According to what I've read, the reason for having this code at the bottom of all your python scripts is to give those files the ability to be used as modules if needed, but not allowing them to automatically execute that code unless they are themselves the "main" file. That way you don't get imported modules auto-executing code on their own, when they shouldn't. If the are imported into another file, and therefore not the "main", then the ' if ' statement won't let them execute. If they are run standalone, they then become the "main" and are executed by the ' if ' statement automatically. It's basically an autorun ' off ' switch if imported, and an ' on ' switch if run by themselves. That's pretty cool. You don't have to use it, but it's probably good practice to include it in all your script files as you develop a larger base of reusable code modules, and you start wanting to import them to various programs.



    Obviously, I'm still a noob at all this, but so far python looks like a great language - much cleaner and straight forward w/o all those brackets and semicolons to keep track of and get in the way. The main stumbling block so far is finding info geared at noobs. Almost everything seems to be written by experienced programmers, for experienced programmers, so it's a bit harder to get into than some other languages... this probably limits wider adoption, which in turn limits the available entry-level info.

    Anyway, I hope this helps someone.
    Last edited by nxgn; 07-03-2009 at 11:40 AM. Reason: clarity & accuracy

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by nxgn View Post
    Ok it's been about 2 weeks and no replies, so I guess nobody else knows what this stuff means either. Also, I've noticed that as I search around for python related stuff, the more it really seems to be a niche language (on the web)... its growing fast I guess, but still has a very limited userbase and its hard to find tutorials & other info when compared to something like PHP (but I guess that's true for most languages).
    what's so hard to find?

    http://docs.python.org/index.html

    there are all in one place, .

    what you ask is very basic and a little confuse, probably that is the reason why nobody answer. I understand that from a wondows user point of view that things seems weird,

    best regards

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thx, yeah python.org is where I've found some of the tutorials I've been working through.

    I know noob questions are annoying, but I'm just starting out so those are the only questions I've got right now. I like Python a lot so I was motivated enough to keep grinding and eventually find the answers to my questions (at least some). I just don't know if most noobs would be; maybe they'd just avoid the headaches and stick to PHP instead. I've been very impressed with the language and I'd like to see Python be even more widespread than it is now - that's really all I was trying to say (but was frustrated and could have said it better). But to get Python to where PHP is now, means tons of more noobs w/ basic questions hacking away on their windows boxes!

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by nxgn View Post
    Thx, yeah python.org is where I've found some of the tutorials I've been working through.

    I know noob questions are annoying, but I'm just starting out so those are the only questions I've got right now.
    there are not annoying, are difficult, usualy there are things we already forget the answer, or hard to remember or maybe which we never ask ourself.

    I like Python a lot so I was motivated enough to keep grinding and eventually find the answers to my questions (at least some). I just don't know if most noobs would be; maybe they'd just avoid the headaches and stick to PHP instead. I've been very impressed with the language and I'd like to see Python be even more widespread than it is now - that's really all I was trying to say (but was frustrated and could have said it better). But to get Python to where PHP is now, means tons of more noobs w/ basic questions hacking away on their windows boxes!
    why to get python where is php? there are a lot of things outside, I don't know if there are better then python and php or not. Everything have good and bad parts,
    search the net for ruby, erlang or haskell,
    btw about python, look to 4suite for example,

    http://4suite.org/index.xhtml

    best regards

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by nxgn View Post
    Ok it's been about 2 weeks and no replies, so I guess nobody else knows what this stuff means either. Also, I've noticed that as I search around for python related stuff, the more it really seems to be a niche language (on the web)... its growing fast I guess, but still has a very limited userbase and its hard to find tutorials & other info when compared to something like PHP (but I guess that's true for most languages).
    It's the same as with any other language. People pick them either for specific needs available from that software or merely because they take to it. I believe the likes of Google and such use mainly Python.


    Quote Originally Posted by nxgn View Post
    #!/usr/bin/env/python

    ...is in fact just for use on a Unix based system. It automatically tracks down the python interpreter wherever it is filed away, making your scripts more portable when jumping from machine to machine. So unless you use Linux (or some other Unix OS) and expect your code to be jumping around to other unfamiliar Unix OS computers, then you can just leave this out of your programs.

    Include it. That denotes which intepreter should be used to run/parse the code. To leave it out is folly, (and poor practice, in my opinion), for the sake of one extra line in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •