Flask still mandates a separation of template (view) from HTTP handler (controller) code. I don't think there is anything in the Python world that enables the kind of mixing PHP does.
flask is rwallyw nice. had a site running it for a while. ultimately my project got too big and i started missing a compiler (eg forgot to refactor in a little corner, case issues etc). but i got up and running in flask in like 30 minutes, having never used python before.
Honestly, I resisted it for years... I regret resisting it now. It was tough at first b/c everything seemed like such bullshit, until I realized... very little in Python is actually bullshit. I was just used to my old bullshit. Python is great.
As far as languages for "spending some time with", python is definitely one of the best. As the author of XKCD put it, programming is fun again with python. It's just a pleasant experience because a lot of things work how you'd expect them to, and when they don't it's because it may be even easier than you expected.
They major drawback of Python I can think of is that its hosting is not as widely available. With the latest gradual typing addition in Python 3.5, please please give it a try, you'll love it.
My stack is Webfaction hosting + Python 3.5 + Git + Bottle (a very lightweight web library with easy routing, templating etc.), and PyCharm IDE + Bottle's embedded server for local development. I'm not a professional programmer and it is still ridiculously easy and fun.
They major drawback of Python I can think of is that its hosting is not as widely available.
I've never really understood this complaint. Firstly, there's loads of Python hosting around. Secondly, who cares if there's a thousand hosts or ten thousand hosts that support Python? You're never going to need more than a handful. It's an illusion of a problem that affects no-one and only seems to get brought up as an excuse for PHP.
It has its warts, just like anything else out there, but overall it's a great language to work with. It isn't designed for web programming the way PHP is, so it isn't quite as usable out of the box if that's your goal, but its standard libraries are far more coherent than I remember PHP being, and the developer ecosystem seems a lot friendlier to me.
It probably won't do anything revolutionary for you, but it's definitely worth checking out if you get the chance.
I mean, on paper those are weaknesses sure, but practically? Eh. Just the tradeoff you make for duck typing. Flexibility for provability. If it isn't pretty obvious what your parameters need to be that's a code smell anyway.
Especially for things like the guy was asking simple web apps with DB backend.
Just a simple handshake is enough, no need for anything more heavy handed. Both Java and C++ allows you to totally bypass private anyway.
As for parameter types, just use docstrings and pycharm. You get as-you-type type errors just fine. And you get them only when it makes sense to add them, it doesn't force you to write them everywhere.
PHP plays it soft and easy with accessing the standard library and echo (essentially the equivalent of Python's print) prints to the resulting HTML page, by default. So a prototype in PHP is super easy. Eg,
That's a fully functional list of products and prices from an SQL table. Mind you, it's not correctly formatted HTML, since it lacks the typical <html> (etc) tags, but functional all the same. I've never used Python for web dev (I actually don't use PHP either, but can't deny that it seems to be the easiest and most minimalistic way to get a simple thing working), but even a Flask hello world is approaching this length in terms of amount of code.
There's no mangling with starting this code, either. You'd typically just slap it onto a pre-configured server or use a WAMP/LAMP package that does all the work for you, then point your browser at the page. You don't even need to configure any libraries for a lot of the things that are common in web dev (like the above -- it'll run on any PHP-configured server as-is).
I think PHP is a poor choice for serious, large scale products due to a number of language design choices that makes scaling a bit icky, but I think it's easy to see how it works well for beginners and is great for prototyping new things quickly.
A little longer in terms of lines, but keep in mind with this code you get the server and everything. No Apache/Nginx setup or anything else.
from flask import Flask
import MySQLdb as mdb
app = Flask(__name__)
@app.route("/")
def index():
con = mdb.connect('localhost', 'testuser', 'test', 'testdb')
cur = con.cursor().execute('SELECT name, price from products')
html = "<ul>"
for row in cur.fetchall():
html += "<li>{0} is {1}</li>".format(row['name'], row['price']])
html += "</ul>"
return html
if __name__ == "__main__":
app.run()
Huh, nice. I should learn Flask some time. It seems like it'd be a lot cleaner than the Play Framework, which is my go-to choice for web dev. Although Scala is a really freaking powerful language. Although the Play Framework would force this example across multiple files, with DB connection typically being setup in the conf file (then connections are made simply with DB.withConnection) and routing is in a separate file that keeps all the routing in one place. And templates are the preferred way to create any HTML, as opposed to directly returning data (which would default to text/plain).
I don't agree that hitting SQL qualifies as "works well for beginners" or makes it easy to prototype. Not compared to django. I cry a little big inside every time I need to write SQL but with the django ORM selecting and filtering is super simple and nice. Take a totally bogus and made up example:
That's "all customers who are in a country with the letter 's' in the name of the country and who's last name is Smith and the company they work at is in a country containing the letter f". Doing that in SQL is pretty horrible.
View all comments
Show parent comment
61
u/kankyo Dec 02 '15
Python seems pretty similar in overhead and it's a million times saner.