| New Post | New Topic |
| Goto page 1, 2 Next | |
| 05 Mar 2009 02:33:29 pm by elfprince13 | Python Crack (or: those features you're hooked on) | Quote | ||||||||||||||||||||||||||
| Feel free to contribute as you see fit.
_ In the interactive python shell, the special variable _ is equivalent to Ans in TI-Basic. It holds the last returned value. classmethod and staticmethod Most object oriented Python code doesn't make use of these two, but they're very useful for declaring static functions inside a class. Normally if you have a class definition like so:
and the interpreter automatically passes blahobj as the self argument to blahblah. Sometimes; however, you want a Java + C style static functions that belong to the whole class, not just to a single instance (generally utility functions related to the purpose of the class). In this case you can declare a function to be a classmethod, which receives the class object it is called on as the implicit first argument; or with staticmethod, which receives no implicit first argument at all. Static methods should be used when the function will always be called on the same class. Class methods should be used when the function will be called on different classes with different implementations of the same function. To declare a class or static method, you have two options. Decorator syntax:
or the traditional way:
unpacking + zip say you want to return multiple values from a function.
min = 1 median = 6 max = 9 This works for tuples and lists. if for some strange reason you want one-tuples, you can create them like this:
and to unpack the one-tuple
You can also unpack lists, tuples, and dictionaries into a function call.
to pack an arbitrary number of arguments into a list you can do the reverse.
The same works for dictionaries and keyword arguments. It's hard to think of trivial examples for keyword arguments, so for this I'm just gonna link you to the python tutorial. http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html Because python is an interpreted language, you can actually dump your programs configuration settings to a text file, and then use the eval() function to read it back later. This should be used with caution, since eval will execute any valid python code in the input. One last thing: zip(). It will take its arguments and zip them together. The easiest way to explain this is to see it in action.
At this point, zlist equals [(1, 4), (2, 5), (3, 6)]. To unzip this back into it's constituent parts, you can do this:
l2 now equals [1, 2, 3] and l1 now equals [4, 5, 6] list comprehensions basically, a one-line way of building a list from some input. for example, say you want a list of the ascii values of every character in a string.
output would now equal [79, 109, 103, 104, 97, 105, 103, 117, 121, 115] You can also conditionally include parts of the list. For example, to remove all the vowels from a string you could do this.
output would now equal "mghgys" magic slicing Most of you should be aware of the ability to slice to the end of a list, or from the beginning of a list (e.g. list[:5] is equivalent to list[0:5] and list[5:] is equivalent to list[5:len(list)]), but there are a couple additional features that are nice. you can use a negative list index to get the last n items in a list. For example list[-5:] will retrieve the last 5 items in a list, and list[:-5] will retrieve all but the last 5. Better still is slicing with 2 colons. list[2::3] will retrieve every 3rd item in list, starting from an index of 2. For example
will return every multiple of 3 between 0 (inclusive) and 100 (exclusive), or to use mathematical notation all multiples of 3 in [0, 100). |
||||||||||||||||||||||||||||
| 05 Mar 2009 03:48:33 pm by magicdanw | Quote | |
| Python scares me... | ||
| 05 Mar 2009 03:56:37 pm by elfprince13 | Quote | |||
care to elaborate? Anything in particular that scares you? |
||||
| 05 Mar 2009 04:03:31 pm by magicdanw | Quote | |
| I think a lot of it is the dynamic typing. It seems useless, inefficient, and could encourage one to code without thinking ahead.
Also, I like curly braces... |
||
| 05 Mar 2009 04:11:44 pm by elfprince13 | Quote | |||||
It's not just dynamic typing (variables can hold values of any type), it's Duck typing, which means that types are essentially irrelevant. Basically you can imitate class A closely enough with class B to pass objects of type B to a function that needs objects of type A without having to extend A.
The only thing I really miss from C-ish languages is the ?: operator, although I just had a brainwave for how to implement it Not having {} leads to much more structured source code, which is a huge boon in preventing code-rot. |
||||||
| 05 Mar 2009 04:26:34 pm by magicdanw | Quote | |||||||
|
||||||||
| 05 Mar 2009 04:35:40 pm by elfprince13 | Quote | |||||||||
the rationale behind duck typing is that if it walks like a duck, swims like a duck, and quacks like a duck you shouldn't care if its a duck or not.
compare:
obviously the first example *could* be structured just as nicely, but we also all know that it won't always be, and everyone has different standards as to how they like code to be laid out when you use curly-braces. |
||||||||||
| 05 Mar 2009 05:01:58 pm by magicdanw | Quote | |||||
There, now isn't that nice? As for the code without braces... Imagine if HTML were like that? Or BBCode? Instead of wrapping things in tags, you just prefaced them with a header. Imagine how quickly things would get ugly! |
||||||
| 05 Mar 2009 05:05:00 pm by elfprince13 | Quote | |||||||
I agree, unfortunately not everyone is so well mannered
HTML and BBCode aren't programming languages, they're markup languages, which have a drastically different use, though quite honestly, I wish HTML did force polite indentation, because a lot of sites are pretty dirty looking if you read through the source. |
||||||||
| 05 Mar 2009 06:28:24 pm by Tari | Quote | |
| My favorite variety of Python crack is Django. With just 4 lines of code, I can implement and interface a new object into my templates.
|
||
| 05 Mar 2009 09:33:26 pm by Kllrnohj | Quote | |||||||||||||
It is the same thing in python, just drop the braces. Oh, and you missed the ; after the return statement
And yes, the above actually runs in python. Semi-colons are perfectly acceptable in python. Go ahead, copy/paste it into an interpreter (after "import math", of course) Oh, and you can get "strong" typing in Python. I had a code snippet somewhere here that you could something along the lines of
And it would only allow you to pass in a type of 'int' for the first parameter, but the second parameter could take a type of 'int' or 'float' @Elf: You mentioned list comprehensions, what about dictionary comprehensions? Don't be hating now My favorite uses of python are its extreme flexibility at runtime. For example:
Now that is self modifying code Or the ability to override just about anything. Example:
|
||||||||||||||
| 05 Mar 2009 09:44:53 pm by magicdanw | Quote | |||||||
|
||||||||
| 05 Mar 2009 10:25:40 pm by elfprince13 | Quote | |||
Don't even talk until you switch from writing utility scripts in Python to multi-threaded data processing apps in Java (not my choice, requirement of the research project). <bad pun>That's what we call a "jar"ring transition </bad pun> @Kllrnohj: I haven't used Python 3.0 yet, so no dict or set comprehensiony goodness for me. |
||||
| 06 Mar 2009 12:22:31 am by Kllrnohj | Quote | |||||
You are missing out. Check out this hotness:
Format, of course, takes a variable number of arguments. First argument is {0}, second is {1}, etc.... |
||||||
| 06 Mar 2009 12:46:32 am by KermMartian | Quote | |||
Oooh, that's actually quite nice. I was going to ridicule with:
|
||||
| 06 Mar 2009 12:52:47 am by elfprince13 | Quote | |||||||
Reasonably cool, but why use format() instead of just print("Book: '%s' by %s" % (b.author, b.format)) |
||||||||
| 06 Mar 2009 10:05:53 am by Kllrnohj | Quote | |||||
Because then you have to specify the type, and that just doesn't make sense in a duck typed language. The {n} method doesn't care what type of object you give it. I could have passed in anything for {0}, and I don't have to reflect that in the print string. Also, you are passing in 2 objects, I only had to pass in one
yeah, but again, you have to know the type, and in C if you get the type wrong you don't get an error stating as such, you get a hard crash. Heck, C++ has an output that doesn't require the type to be specified, so its about time Python caught up in that regard |
||||||
| 06 Mar 2009 12:43:22 pm by elfprince13 | Quote | |||
How do you deal with formatting numbers that you want rounded to some number of significant digits? |
||||
| 06 Mar 2009 01:07:47 pm by Kllrnohj | Quote | |||||
There are a *lot* of cool things that the new format can do. Check it out here: http://www.python.org/dev/peps/pep-3101/ |
||||||
| 06 Mar 2009 03:13:22 pm by elfprince13 | Quote | |
| New Post | New Topic |
| Goto page 1, 2 Next | |
[Switch to Desktop view]
© Copyright 2000-2013 Cemetech & Kerm Martian :: Mobile Design by Alex "comicIDIOT" Glanville
Problems? Issues? Or Suggestions? There's a thread for that!
© Copyright 2000-2013 Cemetech & Kerm Martian :: Mobile Design by Alex "comicIDIOT" Glanville
Problems? Issues? Or Suggestions? There's a thread for that!
