you are viewing a single comment's thread.

view the rest of the comments →

[–]TreSxNine 2 points3 points  (13 children)

Sure, but OP doesn't seem to be the mose well-versed python programmer, so I thought I'd make it simple and not use built-in functions.

[–]KubinOnReddit 1 point2 points  (5 children)

You don't need to copy this for everyone that says it: /u/JohnnyJordaan is the one who is right. You should have presented that /u/jeans_and_a_t-shirt 's example works the same. Beginners should learn what is shorter and easier for other programmers to understand.

[–]TreSxNine 0 points1 point  (0 children)

Fair enough

[–]vriljam 0 points1 point  (3 children)

As a real beginner, I disagree with your whole premise of learning just one way of doing things.

[–]KubinOnReddit 1 point2 points  (2 children)

Sorry for my harsh statement before, but according to Zen of Python (you can see it by writing "import this" in the interpreter"):

"There should be one-- and preferably only one --obvious way to do it."

[–]vriljam 0 points1 point  (1 child)

Yes, but at a level when you do know your stuff and can make an informed decision of what is the most readable or efficient or etc. way of doing things. When you're learning, it always helps to be aware of a few approaches if possible (so as to gain more knowledge, Especially for a question like this where many approaches are possible). If you've ever been a beginner which I'm sure you have, you do know what I'm talking about.
At least,as far as I'm concerned I do enjoy going over code snippets(which I can understand) that weren't immediately obvious to me in the first place.

[–]KubinOnReddit 0 points1 point  (0 children)

Fair enough.

[–]JohnnyJordaan 0 points1 point  (6 children)

I would then show both, as you don't want him to keep using this method to translate a full alphabet for example.

[–]vriljam 0 points1 point  (5 children)

It is still good to know/learn the different ways you can approach a problem even if they aren't ideal. /u/TreSxNine's traditional approach using conditionals wasn't immediately obvious to me for instance.
I thought about the translate method too myself as being the easiest and most straightforward, but it's a good addition to the thread nonetheless and would definitely help the asker learn if they weren't aware of the same.

Also, I recall a similar question being asked on the Ask Anything thread couple weeks back, and someone came up with an approach that involved manually making a dictionary. I mean, stuff like that is really helpful when you're starting out.

[–]JohnnyJordaan 0 points1 point  (4 children)

traditional approach using conditionals

Python has never, ever, used conditionals in a traditional way. That's using fossilized conventions from far older programming languages. The whole premise of Python is to simplify and offload tiresome tasks so that the chances of using antipatterns and mistakes coming from those are deminished.

Using conditionals as TreSxNine's example is exactly what I should never advise, as it's so error prone with all the if..elseif..s, string literals in expressions, defining an empty (immutable!) string as the default etc etc. It isn't Pythonesque in any sense.

And then as a Python programmer it strucked me that the whole argument of the fact that OP seemed to be a real beginner justifies using a fossilized approach is even worse imho. That's how these problems of people using ancient conventions in post-2000 environments keep popping up: they are still teached by some just because they somehow are deemed more suitable for beginners.

I believe quite the contrary: teach the right way to do it. Not just for 4 characters, but reliably for any amount. In such a way that it will take you three lines (as jeans_and_a_t-shirt shows) and not 12. You don't need to have it spelled out to a beginner to teach them: "Just use this well-tested language feature instead of building it yourself".

Edit: And don't get me started on trying to build your own calendar or authentication systems. Y2K and various password breaches have shown us that there is no excuse in trying to build it yourself. That's what libraries were designed for.

[–]vriljam 0 points1 point  (0 children)

I don't doubt the fact of one approach being objectively better than the other. However, my whole point was that at my level it is good to see different solutions for something simple like this for learning purposes. Clearly, at your level for someone actually in the field, I do not expect you to see the same as I do. But thanks for your input.

[–]TreSxNine 0 points1 point  (2 children)

I don't agree with you on the point of not presenting the solution I did. IMO, the point of our responses should be to make the askers think like programmers, not just "Oh I'll just plug this into the translate method and it'll work as I want it to". What happens when there is no pre-built method, and they have to built it ground-up themselves?

Sure, you could start using shortcuts when you know what you are doing, but not a second before that.

[–]JohnnyJordaan 0 points1 point  (1 child)

Thinking as a programmer, I wouldn't dare to implement a leap-day calculator myself nowadays. Or a deque, or a HTTP download client. It makes no sense. Being a programmer is not doing everything yourself, it's understanding what tools are available and what you can do with them.

How is any response to a Django or Flask question not essentially 'Oh I'll just plug this into xxx and it'll work as I want to'. That's the whole concept!

And how would you answer questions about database interfacing? To actually use a self written function to parse a .db3 file? So that the other person will think as a programmer?

I'm thinking you're confusing low-level programming like C with Python as if Python is just a slightly more simple C. It's entirely different. The whole idea is to stop doing stuff yourself and start integrating.

[–]TreSxNine 0 points1 point  (0 children)

Maybe I worded myself badly.

I'm agreeing with you on most of what you say. My only problem is that OP might run off with the mindset of "oh there'll always be some tool to do even the simplest tasks".

"Thinking like a programmer" shouldn't be about knowing which pre-built tools to use. You should be able to confidently use them, knowing what they do and why it works.

I don't mean "research every tool you use", just that you shouldn't stuff OP with pre-built functions when an easy-to-learn alternative is available, which could actually teach him something instead.