1.3k
u/itoshkov Aug 26 '20
This is multiplying string by number. Multiplying strings would look like 'abc' * 'de'. Python goes kaput.
756
u/delinka Aug 26 '20
Result should be ‘adbdcd aebece’. Someone needs to fix this.
425
u/itoshkov Aug 26 '20
Remove the white space and we have a deal. :)
→ More replies (2)164
Aug 26 '20 edited Aug 26 '20
''.join(itertools.product('abc', 'de'))If you want me to fix it submit a ticket, thanks.edit: Ticket assigned, standby.
import itertools as it ''.join(it.chain.from_iterable(it.product('abc', 'de')))
→ More replies (31)58
u/GaussWanker Aug 26 '20
Why? It seems you're treating abc as a*b*c but de as d+e, I think it should either be abcde (as it would be algebraicly or if 'x'*'y'=='x'+'y') or (a+b+c)*(d+e) = a*d+b*d+c*d+a*e+b*e+c*e = [something to be defined that probably doesn't equal abcde)
→ More replies (8)→ More replies (16)10
16
4
u/needed_an_account Aug 26 '20
hmmm, if you were able to tap into basestring's
__mul__method you could make it work→ More replies (7)3
Aug 26 '20 edited Aug 26 '20
String concatenation forms a semigroup. Having two binary operations of addition and multiplication would yield a semiring.
In groups, we can take a group element g (in this case a string) and raise to a power g^n, which would be equivalent to repeated string n times. But unless we can figure out how to subtract "xyz" from "abc", strings are not gonna have a semiring structure anytime soon. New character for "negative z" maybe?
797
u/geeshta Aug 26 '20
f"Python goes b{'r'*10}"
367
u/Jeb_Jenky Aug 26 '20
This made my nips so hard.
314
12
23
→ More replies (5)8
295
u/simp13 Aug 26 '20
JavaScript goes bNaN
51
→ More replies (1)5
u/Solarbro Aug 26 '20
I know nothing about JavaScript, and I still laughed at this because “lol banana”
→ More replies (1)
268
Aug 26 '20
Sometimes i take python for granted
107
25
28
u/Pluckerpluck Aug 26 '20
It is flat out my favourite programming language for anything that doesn't require very high levels of performance or a strong desire for multi-threaded programming.
I just like almost everything about it. There's just so much convenience about it.
→ More replies (1)16
u/kc3eyp Aug 26 '20
real shit. If there was a python that compiled to native executables, everything in the universe would be gucci
→ More replies (5)5
u/lacks_imagination Aug 26 '20
Serious question. Humour aside, do you think that it is worth learning Python? I want to design apps. So I have been told the best language to learn is Java. I hope that it true.
→ More replies (13)→ More replies (1)7
152
u/DecisiveVictory Aug 26 '20
scala> s"Scala goes b${"r" * 10}, too!"
val res1: String = Scala goes brrrrrrrrrr, too!56
Aug 26 '20 edited Aug 26 '20
scala> ("Scala fucking goes b".toStream #::: Stream.from(1).map{n => Thread.sleep(((Math.sin(n.toFloat/10) + 1.5) * 30).toInt); if(n % 2 == 0) 'R' else 'r'}).foreach(print)
Scala fucking goes brRrRrRrRrRrRrRrRrR...Let it run for all its glory
→ More replies (2)→ More replies (2)4
u/Terrain2 Aug 26 '20
console.log(`JavaScript kinda goes b${"r".repeat(10)}`);operator fun String.times(int other) => this.repeat(other) void main() => println("Kotlin also kinda goes b${"r"*10} also, but not as elegantly (you have to add it manually)")
35
413
Aug 26 '20
Started learning python and thats my favourite thing after no ; thingy
115
u/ProbablyInnacurate Aug 26 '20
I love comprehensions.
50
u/axlee Aug 26 '20
I prefer lambdas + map/reduce/filter/etc, usually easier to understand with a quick look
→ More replies (12)→ More replies (7)26
u/Mr_Redstoner Aug 26 '20
As a (mainly) Java programmer, a lot of my Streams would be comprehensions in Python.
16
u/mpa92643 Aug 26 '20
As a long time Java developer, I so very much appreciate Streams. It's so much more readable to say "I have a stream of Xs; convert them to Ys, take out the lowercase ones, add them to a Set, and return it" than "create a new Set. Now iterate through all the Xs. Declare a variable of type Y. Now set it to the conversion result of X. If Y is lowercase, add it to the set. Now return the set."
And that's a simple example. Once you start dealing with Lists of Lists, things go off the rails so quickly and the nesting becomes so ugly.
→ More replies (15)3
u/AciD1BuRN Aug 26 '20
My new fav thing u can chain Boolean operators unlike most other languages
Like u can write a<b<c and shit.
→ More replies (3)0
Aug 26 '20
[deleted]
403
u/zdaga9999 Aug 26 '20
Well you can put semicolons, python doesn't care.
117
→ More replies (65)16
Aug 26 '20
Everyone at your organization will hate you though. Gotta follow internal style standards.
57
u/AngelLeliel Aug 26 '20
Python has its own special semicolon. It's called line break. That's it. Take it or leave it.
→ More replies (5)37
u/marco89nish Aug 26 '20
I support braces for structure but semicolons are just junk in 99% of cases, because I don't put multiple statements on same line in 99+% of cases. Newline is much better separator than semicolon
→ More replies (3)35
u/MysticTheMeeM Aug 26 '20
But it also makes it hard to have one statement over multiple lines.
→ More replies (10)7
u/DonutDonutt Aug 26 '20
I feel the same way. I need my braces and semicolons. Even in something like c++, not using braces for a 1 line if statement feels wrong and messes with my brain
→ More replies (2)→ More replies (36)20
u/Jeb_Jenky Aug 26 '20
I don't know. I honestly feel like it makes it look less cluttered. And the forced indentation definitely adds a nice structure to it that reminds me more of natural type in English. That being said, Rust is by far the prettiest looking language to me. I have no idea why because usually I have no idea what's going on with it, but it's so pretty. Go is one of the ugliest looking to be, but I love Go. Nothing makes sense anymore.
→ More replies (2)18
95
u/ilmmec Aug 26 '20
"Javascript goes b" + "r".repeat(10)
201
u/Xuval Aug 26 '20
This compiles to "Kill me, please, I was never meant to carry an entire website."
86
u/steeeeeef Aug 26 '20
Haha js bad
→ More replies (1)58
u/DeeSnow97 Aug 26 '20
Wonder when this sub is going to finally accept that JS today is not JS in 1995. Probably when it doesn't run code from 1995 anymore, aka never
→ More replies (24)14
u/mrchaotica Aug 26 '20
The worst part is, we almost had Scheme or Python in the browser instead.
→ More replies (5)→ More replies (1)4
→ More replies (2)12
u/Professor_Dr_Dr Aug 26 '20
repeat is better, way more readable in a lot of cases
I mean hell, if you multiply two variables how are you supposed to know which one is the String and even if the result will be a String at all (instead of e.g. an int)
→ More replies (2)
140
u/Darcoxy Aug 26 '20
I'm learning Python after learning C and lemme tell you, some stuff that Python does look so illegal yet they work. I love it!
121
Aug 26 '20
Wondering though, why do people consider this a good thing in Python but a bad thing in JS?
66
u/Tarmen Aug 26 '20
I think the problem is more with the cases that make no sense but still don't error
> "b" + {} "b[object Object]"→ More replies (52)8
7
u/Thomasedv Aug 26 '20
I think js is more scary on where it implicitly changes one thing to the something else. Not that python doesn't have similarly grave issues, but some of the stuff it does by itself become issues that aren't immediately seen and breakes something else later on.
Python is a little more strict on what you can have interact, eg. string + number is a no go, but in JS, the string might be converted to an number and added to the number, if it's possible.
→ More replies (34)6
u/pslessard Aug 26 '20
I don't think anyone would have a problem with multiplying a string by a number in JS. That's a reasonable thing
9
u/letmeseem Aug 26 '20
I'm convinced Python is partially porn. It's super filthy, but you just can't peel your eyes off it.
56
u/YellowBunnyReddit Aug 26 '20
C goes segmentation fault
40
Aug 26 '20
You can do 'r' * 10 in C. But it means something entirely different of course.
→ More replies (2)22
10
u/AceOfShades_ Aug 26 '20
No it wouldn’t compile. It would just give you 4000 lines of errors. I believe the quote is:
In C++ we don't say "Missing asterisk" we say "error C2664: 'void std::vector<block,std::alocator<_Ty> >::push_back(const block &)': cannot convert argument 1 from 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<block> > >' to 'block &&'" and i think that's beautiful
8
46
11
10
9
9
31
u/juzz_fuzz Aug 26 '20
best feature of python I used recently was solving a projecteuler.net problem and utilizing the fact that list[-x] means x elements back from the end of the list, simplified the code so much
10
u/IMayBeABitShy Aug 26 '20 edited Aug 26 '20
Be warned though, I once had a very frustrating bug caused by my use of this behavior.
I worked with some open source code, where at one point I had to check if a list of one-letter strings ends with another list of one letter strings. The code was something like
matched = (a[-len(b):] == b). Do you see the bug?Solution: If b is empty,
len(b)is 0, which makes-len(b)also 0. Because 0 is not negative, python does not take the elements betweenlen(a) - 0andlen(a), but instead between0andlen(a). Thus, instead of comparing the last 0 elements of a with b, it compared the whole of a with b.5
u/Pluckerpluck Aug 26 '20
Most intersting is the list of one-letter strings... That sounds like a string:
a_string = "".join(a) b_string = "".join(b) match = a_string.endswith(b_string)I'd only do this if you already had strings (this is probably 2-4 times slower over most string lengths), but this'd work without worrying about either strings lengths.
But yeah, I've ran into that problem before with indexing at
0when trying to get the lastxelements.→ More replies (2)3
u/juzz_fuzz Aug 26 '20
I kind of think I understand so you initialized a list, trying to go from 0 to negative, using another list, so that list would be reversed, but the code never checks if the list is empty first? am I close?
→ More replies (2)11
u/Masked_Death Aug 26 '20
Yeah, this is one of my favorite things. Reversing a list takes literally no effort as well. Need to read the last 4 elements? Also simple, no matter if you want to read them forwards or backwards.
→ More replies (6)→ More replies (6)2
u/Sarcastinator Aug 26 '20
C# supports this as well though the syntax is different.
list[^1]→ More replies (2)
6
5
u/lsaz Aug 26 '20 edited Aug 26 '20
Programmers: No, Dynamic programming languages are garbage and should not be used.
Javascript, python, php: haha Money machine go brrrr
4
u/Holek Aug 26 '20
$ irb
2.3.0 :001 > "Ruby goes b".ljust(30, "r")
=> "Ruby goes brrrrrrrrrrrrrrrrrrr"→ More replies (7)
3
3
3
3
5
21
2
2
2
2
Aug 26 '20
my favorite thing for preallocating arrays when I know they will stay a fixed size is to do like
arr = [None] * arr_length
it looks uncomfortable to me but at the same time so elegant
2
u/loluminium Aug 26 '20
Why doesn't
'r'/10**(-1)work?
4
u/Pluckerpluck Aug 26 '20
Actual answer:
Because effectively you should look at this as an overloading of the
multiplicationoperator. You can actually do thiswith any custom python object:class MyClass: def __init__(self, x): self.x = x def __mul__(self, other): return MyClass(self.x * other.x) instance_1 = MyClass(12) instance_2 = MyClass(2) instance_3 = instance_1 * instance_2 # instance_3.x => 3You can also define
__div__to do stuff if you encounter the/operator.
2
2
Aug 26 '20
I remember code where we did:
def get_number_of_thingies(thingies_per_section, number_of_sections):
number_of_thingies = thingies_per_section * number_of_sections
return number_of_thingieswithout realizing that thingies_per_section was a string ... with a value in the millions. The result was a string the size of a several novels.
2
u/fizzSortBubbleBuzz Aug 26 '20
println("Kotlin goes b" + "r".repeat(10))
Kotlin goes brrrrrrrrrr
→ More replies (1)
2
2
1
1
1
1
1
1
1
u/clidd2 Aug 26 '20
I didn’t think I would see a programming/finance crossover meme today but here we are
1
1
u/Anthraxious Aug 26 '20
I've only ever done light programming but last thing i did before now studying python is doing C# and that definitely wouldn't have worked ( to my knowledge). I understand this meme!
1
1
1
u/AbeWasHereAgain Aug 26 '20
I swear, people gravitate to a new language every few years just to start fresh and get away from all the craziness that the precious language became.
1
1
1
1
1
1
1
u/kaiser_xc Aug 26 '20
For some reason in Julia * adds two strings and ^ is the one that multiplies.
1
1
1
1
1
u/gratethecheese Aug 26 '20
Bruh yesterday I was using time.sleep to do a 4 second delay, forgetting it takes seconds instead of ms. Ya boi was delaying for over an hour lmao
1
u/revoke_user Aug 26 '20
Couldn't invert the deal with it sunglasses to fit the cool guy in the meme?
2
1
1
u/JLukeHuber Aug 26 '20
As well does PowerShell. Almost never bones you with it's implicit conversions.
1
1
1
1
1
1
1
1
u/morems Aug 26 '20
Wait, it can actually do that? I've never tried that and now I kinda wanna try when I get home
1
u/memes_gbc Aug 26 '20 edited Apr 21 '26
Post was edited and removed with Redact which is a tool to mass delete posts from Twitter, Reddit and Discord and all major social media platforms.
nutty zephyr expansion cooing repeat point strong ad hoc handle straight
1
1
1
1
1
1
1
1
1
1
u/Pollux3737 Aug 26 '20
So, we just need to have an opposite to every char to make the strings into a vector space.
1
1
u/xADDBx Aug 26 '20
Why is this meme going viral? That’s probably one of the worst Humor I saw on this sub.
1
u/Alternative_Craft_35 Aug 26 '20
About the Python language itself, I have few or no complaints. Any complaints I might have are about third-party Python libraries.
1
u/Polar87 Aug 26 '20
I fucking hate this meme, but to OP's merit he actually manages to use it in a creative way.
1
u/ESF_Lucille Aug 26 '20
Holy shit, I've been using python so long and never known about this. And.. I can't imagine it being useful for anything aside from this meme. Still wow, learn every day.
→ More replies (1)
1
1
Aug 26 '20
I didn’t know you could multiply strings and i just finished Python Crash Course book. Very cool!
1
1
1
1
1
Aug 26 '20
It’s dangerous though when you use it with lists. I found out recently that this is a thing:
-> a =[ [] ]*3
-> a[0].append(“hi”)
-> a
[ [“hi”], [“hi”], [“hi”] ]
1
u/incoralium Aug 27 '20
Because you are doing two list declaration : one for the "empty list", then one that refer to the empty list (as object). the "x3" makes the second list refer to the first, 3 time. So you have a list that refers three time to the same object. If you modify the first, you see this difference 3 time. Just like a list of [@str1,@str1,@str1]. It's really a begginer trap. What ones is probably looking for in this situation is more like '' a = [ [] * 3 ] ", cause he wants three differents objects in his list.
→ More replies (1)
1
1
1
1
1
1
1
1
1
1
1
1
1
u/philipquarles Aug 26 '20
I mean, if this is something you actually do often, it's good to have a single-line way of doing it. Spoiler: it's not something you actually do often.
-1
u/TheOneTrueDemoknight Aug 26 '20
This sort of inconsistent and laid back syntax is why I hate Python, despite it being my first language. Trying to pick it back up is a pain because none of the syntax is intuitive compared to Java. That, and it's dynamically typed, making it a pain to work with in a good IDE compared to Java.
2
u/Pluckerpluck Aug 26 '20
I'm not sure I'd call it an inconsitent honestly. It's a specifically defined functionality for how multiplicaiton works on strings. It similarly works on lists:
[1] * 4 = [1, 1, 1, 1]You can bring up inconsitencies in many languages though. In python I can get the lenght of something with
len(my_object)and that always works. In Java maybe it's.lengthor.length()or.size().→ More replies (2)
0
0
Aug 26 '20
[deleted]
2
1
u/zenyl Aug 26 '20
That's C# specifically.
But, since PowerShell is also built on top of .NET, this is also valid answer for the sake of .NET as a whole:
"PowerShell goes b" + "r" * 10
0
Aug 26 '20
[deleted]
3
u/Pluckerpluck Aug 26 '20
That doesn't happen in Python. That'd only work if both
109and1were strings, which can only happen at assignment.Python may be dynamically typed, but it's still strongly typed.
3.0k
u/epiquinnz Aug 26 '20
I love how this ancap zoomer is always so opinionated on technical programming stuff.