|
Yeah, I prefer an OOP/functional combination as well. And when I mean OOP, I don't mean Java-style OOP where your program is basically a big inheritance tree, I just mean taking advantage of the fact that everything is an object but avoiding too much inheritance.
|
# ¿ Oct 24, 2008 18:00 |
|
|
# ¿ Apr 27, 2024 23:22 |
|
ATLbeer posted:
|
# ¿ Nov 2, 2008 23:11 |
|
No need to. Just implement the methods from the interface in those classes and then call them to your heart's content. That's what duck typing is. For instance, several classes in Python implement the methods from the file class, meaning they can act like files, but they aren't subclasses of any particular class just because of that. Python 3000 offers abstract base classes but even in Python 3000, I'd still suggest just implementing the methods.
|
# ¿ Nov 5, 2008 23:05 |
|
That's why Python and Ruby rely on automated testing much more than languages with compile-time type safety.
|
# ¿ Nov 6, 2008 00:03 |
|
The Python example your CS prof provided runs in 10.6 seconds on my machine because it creates a lot of intermediate lists in memory only for the purpose of iterating over them. The example is hardly equivalent to the examples in other languages. You can get a nice speed up if you use this code:code:
Anyway, C and Haskell are compiled, while Python is interpreted. You should ask your CS prof what his point is. If his point is that Python is slow or something, that's a pretty weaksauce claim because speed is just one out of many requirements and it has never been a measure of how good a language is on its own. And besides, I'm sure you could make this run quicker if you used Psyco.
|
# ¿ Nov 8, 2008 17:01 |
|
Here's an implementation that uses some recursion and a list comprehension. tails('hey'), for example, produces ['hey','ey','y','']. So it does that and then checks how many elements in that list start with the needle.code:
code:
code:
hey mom its 420 fucked around with this message at 15:22 on Nov 9, 2008 |
# ¿ Nov 9, 2008 15:15 |
|
Because God wants me to. Haha, but still, I don't like using the tricks with and, although I admit, doing s.startswith(sub) + count(sub, s[1:]) is nice, I've been doing Haskell so much that I totally forgot that you can add together booleans and numbers.
|
# ¿ Nov 9, 2008 22:51 |
|
Also, pretty much never do a catch-all except statement. It catches exceptions like the KeyboardInterrupt exception, which you probably want to leave alone. It's good practice to always specify the exceptions that you're catching.
|
# ¿ Nov 13, 2008 00:56 |
|
Yeah but in the case of except: pass, you're implicitly silencing a bunch of exceptions like KeyboardInterrupt and the like.
|
# ¿ Nov 15, 2008 18:31 |
|
Or for even more code shortness and terseness, you can do imap(operator.sub, t1, t2)
|
# ¿ Nov 21, 2008 20:12 |
|
I'm not so hot on music theory, but maybe you could do something like this:code:
code:
code:
code:
Also I don't think there's much point in putting this into a function, because it doesn't take any parameters or return anything, it just does input and output, so that's better suited to just be in the top level code of the module. hey mom its 420 fucked around with this message at 04:12 on Dec 21, 2008 |
# ¿ Dec 21, 2008 04:00 |
|
It would, but you'd have to pickle and unpickle it all the time, but sqlite3 is perfect for this kind of stuff.
|
# ¿ Jan 2, 2009 18:41 |
|
Basically sqlite is just a database stored within a single file and you can then run queries on that file and everything. Pretty much one of the best pieces of software around today.
|
# ¿ Jan 2, 2009 21:34 |
|
Why are you storing 40k entries per page?
|
# ¿ Jan 3, 2009 00:56 |
|
I don't see what's wrong with this:code:
code:
Also, just doing map(foo, self.genome) won't do a thing because map doesn't modify the list, it just returns a new copy, so you have to do self.genome = map(foo, self.genome) hey mom its 420 fucked around with this message at 16:39 on Jan 10, 2009 |
# ¿ Jan 10, 2009 16:37 |
|
Why are you iterating with a while and two counters of some sort? Also, you don't have to initialize banwords to a list because it becomes a generator in the next line either way. I'd docode:
code:
hey mom its 420 fucked around with this message at 22:37 on Feb 8, 2009 |
# ¿ Feb 8, 2009 22:34 |
|
Depends on the script. If you write the script in a portable way, yeah. If not, well, no. For instance, if you want your code to be more portable, don't hardcode paths and path separators (like blah\blah, because that is blah/blah on unixy systems) but use os.path and so on. But generally, yes, you can do that. I'd suggest just writing the script first and then posting it here to get people's thoughts on how portable it is.
|
# ¿ Feb 19, 2009 18:38 |
|
Are you sure that's the exact code? Thiscode:
|
# ¿ Feb 24, 2009 23:11 |
|
Maybe he's running it with the perl command, haha.
|
# ¿ Feb 25, 2009 00:00 |
|
This is a good post about it.
|
# ¿ Mar 1, 2009 23:06 |
|
Awesome talk!
|
# ¿ Apr 3, 2009 17:16 |
|
The Haskell solutions use higher-order functions all the time and they're very much different from the C or Java solutions. Project Euler is good for getting used to the syntax and some basic idioms of a language. I used Project Euler when learning Haskell and I think it's especially good for learning it because it makes you use infinite lists, higher-order functions, laziness and forces you to think in a functional instead of an imperative manner. Anyway, I think it's good for getting warmed up with a language. But once you've solved 80 or so problems, it's not about whether you can implement the solution in your programming language of choice, it's about whether you know a mathematical trick or method behind some problem, so that's why I stopped solving them around then.
|
# ¿ Apr 24, 2009 20:14 |
|
That's normal behavior. In order for \ to be interpreted as an actual character instead of an escape sequence, it has to be escaped with, well, \. When you're doing Windows paths in Python, they always look like "C:\\blah\\blah", or you can prefix your string literals with r, then \ isn't treated as an escape sequence, so you can also do r"C:\blah\blah"
|
# ¿ Jun 21, 2009 13:18 |
|
k = [Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40).grid(row=x,column=y) for x in range(4) for y in range(4)]
|
# ¿ Jul 3, 2009 01:35 |
|
Yes indeed!
|
# ¿ Jul 3, 2009 01:45 |
|
But he was saving them in the first example, sooo
|
# ¿ Jul 3, 2009 11:19 |
|
Can you post the exact code to reproduce that? My guess is that the items that are in those lists don't have proper __eq__ methods. Cause this works fine:code:
code:
That scoping rule is called shadowing and that's just the way scoping works in Python. I think that if you could modify variables in parent scopes, it would lead to a whole lot of headaches: shadowing makes it so that you capture variables in the parent scope but you can't modify them. It wouldn't be cool if you defined some variable and then called a function only to realize that the function has changed the value of your variable.
|
# ¿ Feb 25, 2010 23:59 |
|
Lamacq posted:Can you elaborate a little on this, and/or show me where in the python docs I can read up on this syntax? B/c I don't really understand what's going on here. http://www.python.org/dev/peps/pep-0318/ They're decorators, all they do is take a function and return a function. So doing: code:
code:
|
# ¿ Feb 26, 2010 17:27 |
|
A basic use of decorators is something like this:code:
code:
code:
code:
|
# ¿ Feb 27, 2010 13:40 |
|
tripwire posted:To set the first row: The first seems fine since 0, is (0,), but isn't the second assignment a syntax error?
|
# ¿ Mar 1, 2010 01:40 |
|
Python tries to avoid this kind of stuff because of the whole readability thing and that there should be only one way to write something. I guess you could hack something together by accessing the global dictionary, but blehh.
|
# ¿ Mar 2, 2010 21:54 |
|
|
# ¿ Apr 27, 2024 23:22 |
|
Why some people want everything to be a method instead of a plain function I will never understand.
|
# ¿ Mar 11, 2010 22:35 |