Starting Out With Python

First off, hello Infidel (yes you have zapped my curiosity.)

Listen I looked at the www.python.org site and see alot of stuff. Im impressed. I downloaded Python 2.2 and the Win32 COM thing. I played with it for a little bit and I am not really sure where to begin.

Could anyone guide me through a simple example using the compiler and then a real simple example using the Win32 portion, so that I can whet my apetite. You could direct me to an example program online for instance, so I could dissect it and ask questions later.

OR can someone recommend a good starting path of articles I can read. Mainly starting with how to use the compiler and migrating to learning the language syntax and basics, then the hard-core Object Oriented aspects. Keep in mind im using Windows 2000, so I will probably concentrate more on Win32. Im not ready to go all googly eyes yet so I probably wont pick up a book just yet.

Any help for a Visual Basic Veteran, BUT a Python newbie will be appreciated.

Thanx.

Comments

  • : First off, hello Infidel (yes you have zapped my curiosity.)
    :
    : Listen I looked at the www.python.org site and see alot of stuff. Im impressed. I downloaded Python 2.2 and the Win32 COM thing. I played with it for a little bit and I am not really sure where to begin.
    :
    : Could anyone guide me through a simple example using the compiler and then a real simple example using the Win32 portion, so that I can whet my apetite. You could direct me to an example program online for instance, so I could dissect it and ask questions later.
    :
    : OR can someone recommend a good starting path of articles I can read. Mainly starting with how to use the compiler and migrating to learning the language syntax and basics, then the hard-core Object Oriented aspects. Keep in mind im using Windows 2000, so I will probably concentrate more on Win32. Im not ready to go all googly eyes yet so I probably wont pick up a book just yet.
    :
    : Any help for a Visual Basic Veteran, BUT a Python newbie will be appreciated.

    Cool! Welcome aboard.

    The standard tutorial should be under
    Start -> Programs -> Python 2.2 -> Python Manuals.

    When you installed the win32 extensions, there should have been a small tutorial installed at
    Start -> Programs -> Python 2.2 -> COM Readme
    That page has a link to the documentation which has some good examples.

    First off, python doesn't have a compiler. Since it's technically a "scripting" language, it's interpreted. Since you got Python 2.2, the interpreter should be something like C:Python22python.exe (if you used the default paths).

    Normally to run a python script, you would pass it as the first argument to the interpreter:

    C:Python22python.exe myscript.py

    Windows should have .py files registered if you ran the installation package, so you can just double-click a .py file and it will execute in the interpreter. Windows also recognizes the .pyw extension and will execute those scripts without displaying a command box.

    One of the coolest things about the Python interpreter is that you can run it interactively (kind of like the "immediate" window of VB, only far more powerful). There are a couple of ways to do this. Either open a command box and run the interpreter there, or choose IDLE from the start menu, or since you installed the win32 extensions, there is PythonWin which gives you an MDI interface. I prefer PythonWin, so I would start with that. PythonWin also gives you easier access to the COM stuff.

    I really recommend starting with the standard tutorial. I'll check back here periodically during the day to see if you have any questions.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • [b][red]This message was edited by iwilld0it at 2003-6-4 18:20:14[/red][/b][hr]
    : : First off, hello Infidel (yes you have zapped my curiosity.)
    : :
    : : Listen I looked at the www.python.org site and see alot of stuff. Im impressed. I downloaded Python 2.2 and the Win32 COM thing. I played with it for a little bit and I am not really sure where to begin.
    : :
    : : Could anyone guide me through a simple example using the compiler and then a real simple example using the Win32 portion, so that I can whet my apetite. You could direct me to an example program online for instance, so I could dissect it and ask questions later.
    : :
    : : OR can someone recommend a good starting path of articles I can read. Mainly starting with how to use the compiler and migrating to learning the language syntax and basics, then the hard-core Object Oriented aspects. Keep in mind im using Windows 2000, so I will probably concentrate more on Win32. Im not ready to go all googly eyes yet so I probably wont pick up a book just yet.
    : :
    : : Any help for a Visual Basic Veteran, BUT a Python newbie will be appreciated.
    :
    : Cool! Welcome aboard.
    :
    : The standard tutorial should be under
    : Start -> Programs -> Python 2.2 -> Python Manuals.
    :
    : When you installed the win32 extensions, there should have been a small tutorial installed at
    : Start -> Programs -> Python 2.2 -> COM Readme
    : That page has a link to the documentation which has some good examples.
    :
    : First off, python doesn't have a compiler. Since it's technically a "scripting" language, it's interpreted. Since you got Python 2.2, the interpreter should be something like C:Python22python.exe (if you used the default paths).
    :
    : Normally to run a python script, you would pass it as the first argument to the interpreter:
    :
    : C:Python22python.exe myscript.py
    :
    : Windows should have .py files registered if you ran the installation package, so you can just double-click a .py file and it will execute in the interpreter. Windows also recognizes the .pyw extension and will execute those scripts without displaying a command box.
    :
    : One of the coolest things about the Python interpreter is that you can run it interactively (kind of like the "immediate" window of VB, only far more powerful). There are a couple of ways to do this. Either open a command box and run the interpreter there, or choose IDLE from the start menu, or since you installed the win32 extensions, there is PythonWin which gives you an MDI interface. I prefer PythonWin, so I would start with that. PythonWin also gives you easier access to the COM stuff.
    :
    : I really recommend starting with the standard tutorial. I'll check back here periodically during the day to see if you have any questions.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :

    I whipped through some documentation and I must admit I am stoked about
    how strings and list are handled. I love that slice notation and the fact that you can slice strings from the end as well. Anyways, I got to
    functions and am a little frustrated.

    I cant seem to get the value of say like the List objects Count function to print properly. Please tell me if im handling functions
    incorrectly.

    [code]
    >>> mylist = [1,2,3]
    >>> mylist.count

    [/code]

    I tried ...

    [code]
    >>> ct = mylist.count
    >>> ct(mylist)
    0
    [/code]

    But the count comes up zero. I guess im not sure how premade functions and custom functions work.

    Also, how do i program a script in PythonWin editor without it being
    interpreted as I go along. I kinda want to write a complete script then feed it all at once to the interpreter.

    Thanx



  • [b][red]This message was edited by Moderator at 2003-6-5 7:29:40[/red][/b][hr]
    : I whipped through some documentation and I must admit I am stoked about
    : how strings and list are handled. I love that slice notation and the fact that you can slice strings from the end as well.

    Yeah, the basic Python data types are really cool.

    :Anyways, I got to
    : functions and am a little frustrated.
    :
    : I cant seem to get the value of say like the List objects Count function to print properly. Please tell me if im handling functions
    : incorrectly.
    :
    : [code]
    : >>> mylist = [1,2,3]
    : >>> mylist.count
    :
    : [/code]
    :
    : I tried ...
    :
    : [code]
    : >>> ct = mylist.count
    : >>> ct(mylist)
    : 0
    : [/code]
    :
    : But the count comes up zero. I guess im not sure how premade functions and custom functions work.

    Good question! To call a function, you need the () after it:

    >>> mylist = [1,2,3,4,5]
    >>> mylist.count(2)
    1
    >>>

    The count function actually takes an argument and then returns the number of times that value appears in the list. If you want the number of elements in the list, you need the builtin len() function:

    >>> len(mylist)
    5
    >>>

    I'm not sure why length() isn't a method of lists. Just one of those remnants from long ago, I suppose.

    Functions in Python are a sort of object. Just referring to their name (as you did without the parentheses) returns the equivalent of a pointer to that function. That's why you got "" printed out. Just think of () as the "call" operator. You always need parentheses to call a function.

    One neat thing about this aspect of Python is that you can alias functions very easily:

    [code]
    def callback(arg):
    print arg

    def call(func, data):
    if callable(func): func(data)

    call(callback, 'foobar')
    [/code]

    See? when I call the "call" function, I pass is the callback function and a string. Within the "call" function I use the alias "func" which points to the callback function. It's very cool if you ask me. And quite useful, especially when you get to GUI stuff. This is what happened when you set ct = mylist.count. You created a new object that pointed to the count method of the mylist object. The reason it returned zero is because you passed it the list, and the list contained 0 instances of itself.

    : Also, how do i program a script in PythonWin editor without it being
    : interpreted as I go along. I kinda want to write a complete script then feed it all at once to the interpreter.

    Another good question. Just hit "ctrl-n" or select File->New or hit the "New" toolbar button. A dialog should pop up asking if you want a new Python script or a new Grep. If you don't know what "grep" is, it's a UNIX utility that searches for strings in files. Choose Python script and you should get a blank document. enter your code, save it, and then you can either run it from Windows explorer, or by pressing the toolbar button of the little man running. Usually if I'm just working on simple scripts, I run it within PythonWin. If it starts getting more involved, especially importing from modules that I'm working on, I run it from a batch file. Sometimes if you're making changes in multiple modules at the same time, re-running your script won't pick up the changes in some of the modules because of the way that Python caches imports. There are other things you can do to clear out the "memory" so everything works well, but I find it's easy to just run a .bat file so a new interpreter is started fresh.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]



  • Here's a little example to demonstrate more python powers. In our previous exchange, I showed how lists don't have a "length" method. Well, you can subclass the list type and give it one:

    [code]
    class mylist(list):
    def length(self):
    return len(self)
    [/code]

    At least you can do this in Python 2.3 (I have the 2.3 beta). I can't remember if 2.2 allowed this yet. If not, there's another way.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • Sorry I ask alot of questions.

    Can you show me a sample of such a Batch file and/or how to make one. Im assuming this batch file calls some executables with the correct switches.

    BTW, that function callback is nifty. Much neater than c++ function pointers and actually easier to set up than .NET delegates.

    Thanx
  • : Sorry I ask alot of questions.
    :
    : Can you show me a sample of such a Batch file and/or how to make one. Im assuming this batch file calls some executables with the correct switches.
    :
    : BTW, that function callback is nifty. Much neater than c++ function pointers and actually easier to set up than .NET delegates.

    This is what I do in my "run.bat":
    [code]
    C:Python23python.exe [path to .py script]
    pause
    [/code]

    That's it. This allows me to run my script in a "fresh" interpreter to avoid any problems with importing modified modules. The pause keeps the command box open so I can examine any exception traceback if there was an error.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories