7 ms·
So, this shortens down to "I don't want people touching my private class members", "I want multi-line lambdas", and "I want types". WRT private class members,
by falcolas 9y ago
So, this shortens down to "I don't want people touching my private class members", "I want multi-line lambdas", and "I want types".
WRT private class members, I can understand why this might be frustrating for library writers. But it's just so damned useful to be able to reach into a library and get functionality the library's writer didn't think I'd need, that I'll personally never consider this to be bad.
Multi-line lambdas would certainly be nice, but most of it can be handled with a scope-level function. It adds, on average, one line of boilerplate. As for syntax, no need to use Ruby's syntax, just incorporate parenthesis (a well established continuation construct within Python):
with(open("foo.txt"), lambda f: (
print(f)
print("I am a teapot")
))
The sinatra example doesn't really make the writer's argument about decorators to me; since the Flask example exposes a bit more functionality for only two lines of code - I now have a distinct app object, and can create more, or explicitly access attributes of the app itself. I can even add authentication to that function with just two lines:
auth = flask.ext.httpauth.HTTPBasicAuth()
@app.route("/")
@auth.login_required
def handle_route():
return "I am a tea pot"
No, Python isn't Ruby; it doesn't support the same degree of metaprogramming. That's good, IMO. Metaprogramming is the source of exponentially more technical debt (and bugs) than is reasonable, frankly.
As for the lack of a type system, yeah, that is definitely Python's biggest weakness for large programs. A lot of it is resolved with the type hints in Python3, but a lot of it is just Python itself. Love it or hate it, that's how Python is, was, and will be.
As for the example error - there is a typo. Of course it's going to throw a traceback. The error message is even more explicit than I expected, frankly. It pointed out the missing function name, which would make the typo rather easy to find.
- pyrale 9y ago> But it's just so damned useful to be able to reach into a library and get functionality the library's writer didn't think I'd need, that I'll personally never consider this to be bad. I was ready to accept this as a matter of opinion, but then with this : > Metaprogramming is the source of exponentially more technical debt (and bugs) than is reasonable, frankly. Once you've jumped the shark and play with introspection and internals, are you really going to make the point that metaprogramming (or almost anything else, for that matter) should be banned ?
- falcolas 9y ago> are you really going to make the point that metaprogramming should be banned ? As a fan of the concept of the "Catfish" developer (even though I'm a "corporate drone"), absolutely. Or, if not banned, kept to an absolute minimum. It's like the preprocessor in C. You can do anything and everything in it... but should you? You can be exceedingly clever with metaprogramming, but clever code is exceedingly hard to read and maintain. Give me boring, explicit code any day of the week.
- flavio81 9y agoMetaprogramming goes way beyond what the C preprocessor does. Metaprogramming also cannot necessarily be "hard to read and mantain". Take a look at macros in Common Lisp. They are almost the same as a normal function. Yet they do metaprogramming. > Give me boring, explicit code any day of the week. With macro metaprogramming you can eliminate boilerplate code / unnecessary copy-paste repetition of code, and this does improve maintainability of the code. At the same time, replacing 10 repetitions of the same big boilerplate code (similar code, written with slight variations each of the 10 times) with 10 very simple (one line each) calls to a macro, improves readability: because you will easily read what is the difference between those 10 calls each.
- joshuamorton 9y agoMacro metaprogramming can (and in practice often does) break ctrl+f search. If I want to find the definition of PREFIX_varname, I'd expect to grep for "PREFIX_varname", but often, the correct solution is to search solely for varname, because PREFIX_varname is defined by a call to MACRO(varname).
- KirinDave 9y agoMaybe don't use such a primitive editor integration? I'm not trying to dunk on your editor, but people have had this problem solved since the early 90s. If your programmer's editor doesn't have some affordances for your language, then it's not a programmer's editor at all, it's a text editor.
- baq 9y agoRe types - actually, mypy is getting very good, i highly recommend checking it out. It smells a bit like ML/Rust except there's no pattern matching.
- nerdponx 9y agoAt my previous company, we ended up writing Python code that looked an awful lot like Scala or Java: class PricePredictionCalculator(): def __init__(self, price_prediction_calculator_settings: 'PricePredictionCalculatorSettings'): self.x = price_prediction_calculator_settings.x self.y = price_prediction_calculator_settings.y def calculate_something(self) -> int: return self.x + self.y class PricePredictionCalculatorSettings(): def __init__(self, x: int, y: int): self.x = x self.y = y It was actually kind of nice. It allowed rapid prototyping without tons of boilerplate, and then tacking on the boilerplate at the end. Mind you, I was a data scientist, not a full-time engineer. There was also a lot of room for stylistic differences and varying opinions.
- proaralyst 9y agoRegarding your variable naming: why `price_prediction_calculator_settings' and not, say `settings'? Same with the type; why not: class PricePredictor: class Settings: def __init__(self, x: int, y: int): self.x = x self.y = y def __init__(self, settings: Settings): self.x = settings.x ...
- nerdponx 9y agoThat battle was lost before I even had a chance to fight it.
- backslash_16 9y agoI'm curious why about why you don't like that name? I'm asking because I'm usually torn between short names that assume the reader can understand them and long overly descriptive names that (in theory) require less implicit understanding. Is the name price_prediction_calculator_settings too long? If we call it settings I can see an issue come up if we need to pass in another type of settings object calculator_format_settings or something like that.
- KingMob 9y ago
- KirinDave 9y agoThere is no excuse for missing multi-line lambdas other than negging on programmers.
- xapata 9y agoReally, is it that hard to come up with a name for your function?
- KirinDave 9y agoI don't even understand why your question is relevant.
- rjeli 9y agoIf you want a multi line lambda: def print_each_twice(xs): map(lambda x: print(x) # whoops, impossible You can define a local function: def print_each_twice(xs): def print_twice(x): print(x) print(x) map(print_twice, xs) The former is more intuitive to write. The latter reduces nesting, makes you name the function, and improves readability IMO. It's currently easy to scan blocks of Python and see each indentation level starting with one of a few keywords (class, def, for, with). Adding multi line lambdas would make the ultra simple structure harder to quickly parse.
- KirinDave 9y ago> If you want a multi line lambda: I don't want this though. This doesn't do what multi-line lambdas do. It can never do what lambdas do. It's not a lambda function if it's named and fully pre-computed. > Adding multi line lambdas would make the ultra simple structure harder to quickly parse. This is just about the least compelling argument I've ever heard. "The parser writers are not going to have fun."
- rjeli 9y agoI meant to parse mentally. Control flow in Python is very easy to mentally parse at a glance. Powerful lambdas get rid of that. It's an extremely contrived example, but from the code I posted, it should just be: for x in xs: print(x) print(x) Much easier to read because there's only one way to write it. Hence GVR shunning map/reduce/filter.
- tome 9y agoHuh? What do you mean? That multi-line lambda is not standard Python is it?
- falcolas 9y agoNo, it's not included. I meant: "It would be nice; here's my suggestion for syntax"
- zenhack 9y ago> WRT private class members, I can understand why this might be frustrating for library writers. But it's just so damned useful to be able to reach into a library and get functionality the library's writer didn't think I'd need, that I'll personally never consider this to be bad. I can only really think of one instance where I've used this, and it was to work around an outright bug in the library (which was unmaintained, and I was using out of a certain amount of desperation). FWIW, most of the complaints around private in my post would actually be solved just by having a separate namespace for "private," even if there was a back door. I'd be pretty willing to forgive that. > Multi-line lambdas would certainly be nice, but most of it can be handled with a scope-level function. In the absence of the with statement can you really imagine yourself writing this all the time: def _with_body(f): print(f) print("I am a teapot") with(open("foo.txt"), _with_body) ? Strikes me as pretty ugly, despite not actually adding much code. > As for syntax, no need to use Ruby's syntax, just incorporate parenthesis (a well established continuation construct within Python): Yeah, that's a bit nicer. > As for the example error - there is a typo. Of course it's going to throw a traceback. The error message is even more explicit than I expected, frankly. It pointed out the missing function name, which would make the typo rather easy to find. Yeah, that was kinda my point; I was comparing this to what happens if you try to "take duck typing to heart", where you don't get the error until you try to call request. The earlier you find out about mistakes the better.
- falcolas 9y ago> can you really imagine yourself writing this all the time Honestly, no. But not because of any measures of beauty, but because I just don't do callbacks all that often. try: f = open("foo.txt") print(f) print("I am a teapot") finally: try: f.close() except: pass is what I would be more likely to write (and did write, before context managers were a thing), if the "with" statement didn't exist. It's ugly as sin, but I can put it in front of anybody and they will understand what's going on. I can also tell at a glance that it is correct.
- hangonhn 9y ago"WRT private class members, I can understand why this might be frustrating for library writers. But it's just so damned useful to be able to reach into a library and get functionality the library's writer didn't think I'd need, that I'll personally never consider this to be bad." I'll second that. I'll share my own experience to give some perspective. Older versions of the JDK had an LDAP library that for some reason I can't understand (actually pretty sure it was a mistake because it was changed in subsequent version) specified a File as the type instead of a Stream for its configuration file. There was no way for me to modify the configuration during run time without restarting the JVM. This wasn't acceptable to the software we were writing because it supported multi-tenancy. What did I end up doing? I used introspection to grab the "private" configuration table (Vector or Hashmap?) and wroten an API around it to allow modification on the fly. Lesson here is that "private" things aren't really so private and Python style discretionary "private" variables would have required a lot less work.
- sevensor 9y agoIs it possible that encapsulation just doesn't work, in general? It really boils down to a library author saying "trust me, this'll work great!" Sometimes it does, and sometimes it doesn't. In that light, Python's convention that a leading underscore means "probably better avoided" seems pretty reasonable. This is especially true for Python, where the type system gives you no guarantees about anything.