cosc 2p91
play

COSC 2P91 Lets Python Some More! Week 8a Brock University Brock - PowerPoint PPT Presentation

COSC 2P91 Lets Python Some More! Week 8a Brock University Brock University (Week 8a) Lets Python Some More! 1 / 16 Remember Python? Lets all remember Python for a moment Weve gotten a basic look at the syntax (primarily in the


  1. COSC 2P91 Let’s Python Some More! Week 8a Brock University Brock University (Week 8a) Let’s Python Some More! 1 / 16

  2. Remember Python? Let’s all remember Python for a moment We’ve gotten a basic look at the syntax (primarily in the interactive mode, but we haven’t really gotten into writing actual programs yet. Python scripts follow largely the same design philosophy as C programs: Modular code ◮ Achieved via modules , instead of translation units Awareness of scope (including local and global variables) Though Python also arguably has better support for things like encapsulation. Brock University (Week 8a) Let’s Python Some More! 2 / 16

  3. Writing Python scripts For the most part, you simply write into a plain text file what you would have written into the interactive prompt The traditional extension is .py In a command-line environment that’s set up appropriately, you can run your script by typing python myprogram.py ◮ Technically, you could load your file directly from the interactive prompt, but for now, let’s just say “don’t” Of course, what we’d really like would be to be able to simply run a script directly (like an executable). Brock University (Week 8a) Let’s Python Some More! 3 / 16

  4. We should do that thing That executable thing Obviously, you’ll need to chmod your script. I’m assuming we know how to do this. Beyond that, we’ll need to have the first line in the file be commented , but contain a bang, and the executable that bash should use to run the script (it’s a very odd, but common, ’nix convention). e.g. #!/usr/bin/python or #! /usr/bin/env python Compare this to how bash scripts traditionally begin. Brock University (Week 8a) Let’s Python Some More! 4 / 16

  5. So, what happens when you run a python script? Well, quite simply, it starts doing what it’s told Declared functions are available throughout the rest of the script* Variables declared without indentation are global* Code entered without indentation is run immediately, in sequence ◮ Though not strictly necessary, try to have all of your function declarations in one part, and all of your script-level code in the other ◮ It can be hard to read if you have code, function declaration, code, function declaration, etc. Import statements go at the top, but we aren’t at that point quite yet Python might use a Just In Time compiler to compile your script as you run it Let’s look at our first example, shall we? Brock University (Week 8a) Let’s Python Some More! 5 / 16

  6. Scope Scope is a little quirky in Python. Generally, variables are either global, or local to their blocks. As with the languages we’re used to, you can’t reach sideways into a sibling scope, and you can’t reach further in. The two catches are nested functions, and trying to reach out . Example time! Brock University (Week 8a) Let’s Python Some More! 6 / 16

  7. Lambda Expressions Lambda expressions in Python are... pretty much like lambda expressions elsewhere. Also known as anonymous (or unnamed) functions , they’re usually single-use functions that you declare for some limited purpose, and don’t reuse enough (or more likely don’t use independently) to warrant giving them names They’re commonly used for data-processing, text-processing, comparisons, etc. The syntax is simple: lambda x,y: x*y Of course, if you try typing that into the interactive prompt, it technically works , but is entirely useless Lambda functions make no sense unless you’re either assigning them to a variable (for later use), or passing them as parameters to something that’s going to supply the arguments to them Example time! Brock University (Week 8a) Let’s Python Some More! 7 / 16

  8. Closures Assuming I kept on-topic, I just showed you something that was (more or less) equivalent to a closure . This isn’t 2P90/3P05/2P05, but basically closures keep some measure of a context; they remember variables not just within the defined function, but within the enclosing scope For Python, this is primarily interesting when we want to have a function that can create another (customized) function, or if we simply need a portable function that can retain additional information The last lambda example we saw was an example of this, but it can also be used for nested functions. Brock University (Week 8a) Let’s Python Some More! 8 / 16

  9. List comprehensions Suppose we wanted to create a list consisting of the last letters of each word in a list of words. Of course, we could very easily write simple, clean code to achieve this explicitly. Or, we could use a list comprehension : a shorthand for building a list. What time is it? ◮ It rhymes with lekshmample mime ! Brock University (Week 8a) Let’s Python Some More! 9 / 16

  10. Generator expressions Okay, so maybe a list comprehension would solve all of your problems, except the memory requirements for an excessively large list are just too high For example, what if we needed a million values, but only one at a time? This is where generator expressions and generator functions come in ◮ generator expressions have the same syntax as list comprehensions, and create generators ◮ xrange is actually a special case of a generator Let’s look at (you guessed it) an example! Brock University (Week 8a) Let’s Python Some More! 10 / 16

  11. Generator functions Okay, so we know what generator expressions are, but... generator functions? Well, to some extent, generator functions are just a simplified version of generators ◮ (more accurately, they’re shorthand for creating generators) A generator function is simply a function that can behave as a coroutine , instead of a subroutine ◮ I assume we know what these words mean, and thus no explanation is necessary, yes? The way we can achieve this in Python is with the yield statement Brock University (Week 8a) Let’s Python Some More! 11 / 16

  12. Modules Like any good modern language, it doesn’t make sense to just take anything a programmer might need, and just dump it all into the namespace every time the runtime environment starts. That is, just as Java divided its included tools into packages, and C (to some extent) divides tools into libraries, Python divides code into modules . In the simplest sense, each of the examples we’ve looked at thus far could be considered modules They have their own namespaces , variables, and functions Let’s compare two ways to load one of our examples (let’s say our fibonacci example) into the interpreter ◮ import slide11c ⋆ Seriously, don’t forget about the help function here! ⋆ Also, dir is useful! ◮ from slide11c import * Brock University (Week 8a) Let’s Python Some More! 12 / 16

  13. Module (cont) The way things like scope and visibility are actually managed is pretty clever: each module has its own symbol table , which keeps track of what’s available to each module. Normally, each symbol table is private to that module, but the from X import Y variant imports members of the module directly into the receiving module’s symbol table. Of course, the reason we have to do this explicitly is obvious: otherwise we’d pollute our namespace far too quickly! Neither here nor there, but if you need to import an alternate module to replace a previous one, and don’t want to change the rest of your code, you can import under a different name: import dealie as grobble (It’s good for pickling?) Brock University (Week 8a) Let’s Python Some More! 13 / 16

  14. Modules Is it a program? A helper module? Porqu´ e no los dos? As you might recall, if we have some top-level code in a source file, then that code will run automatically upon loading. That makes sense if the module is to act as the ‘main program’ What do we do if we just want to borrow some of its functions? ◮ We need to be more clever in how we write the module in the first place ◮ There are some properties that each module has innately ⋆ Let’s try using dir() right now on a fresh invocation of the interpreter ⋆ Notice the name ⋆ Try importing the example, and checking its name ◮ Basically, if a script checks if its name is name , and only then proceeds to execute code, it can serve dual-purposes Brock University (Week 8a) Let’s Python Some More! 14 / 16

  15. Modules A final thought Of course, there are plenty of included libraries that come with Python. These are called the standard modules sys — related to the runtime environment os — provides access to numerous functions of the underlying operating system time — good for keeping track of time or sleeping re — regular expressions socket — for... sockets? cgi — for CGI pickle — I swear, this isn’t a joke random — provides ducks math — I can stop explaining these, right? Just... just look here: https://docs.python.org/2/library/ Brock University (Week 8a) Let’s Python Some More! 15 / 16

  16. Questions? Comments? What’s the best chocolate, dark or really dark? Brock University (Week 8a) Let’s Python Some More! 16 / 16

Recommend


More recommend