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. :)
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 (7)→ More replies (1)7
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)
22
→ More replies (1)7
u/auser9 Aug 26 '20
Well considering ‘a’+’b’+’c’ gives you ‘abc’ I would say string attach by addition, and multiplication is undefined behavior
10
31
u/absurdlyinconvenient Aug 26 '20
why not dabceabc or adebdecde? Nevermind that multiplication always should be commutative
Or, let's go totally rogue and multiply the character codes and retranslate, fuck it
26
u/Phrodo_00 Aug 26 '20
Nevermind that multiplication always should be commutative
Only in commutative rings like ℤ and ℝ
6
91
→ More replies (7)4
16
u/Acalme-se_Satan Aug 26 '20
I take it you haven't seen Julia yet.
11
u/neil-lindquist Aug 26 '20
I mean, Julia just uses
*for Python's string addition and^for Python's string-int multiplication. So, it's really the same thing, just changing the operator symbol.→ More replies (3)4
u/needed_an_account Aug 26 '20
hmmm, if you were able to tap into basestring's
__mul__method you could make it work3
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?
2
u/rmTizi Aug 26 '20
To be more pedantic, this looks more like multiplying a char by a number.
No idea if python has that concept though.
→ More replies (1)→ More replies (4)2
u/MrHyperion_ Aug 26 '20
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'str'
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
u/xDarkFlame25 Aug 26 '20
Ah yes fstrings, the ultimate fetish.
→ More replies (2)155
u/AinsleyBoy Aug 26 '20
I fucking love fstrings. I use them so much
→ More replies (2)42
u/Moldy_pirate Aug 26 '20
Is there ever a reason to use a “regular” string rather than an f”string?
→ More replies (19)12
23
u/marmoshet Aug 26 '20
laughs in 3.6
3
u/mxzf Aug 26 '20
You still have
.format(), even in 2.x. Which is still way better than formatting strings with%.8
0
Aug 26 '20
[deleted]
37
u/pterencephalon Aug 26 '20
I'm enjoying going through my old code and changing things to fstrings. Instead of actually fixing the real problems with the code.
13
u/jad2192 Aug 26 '20
Same, I go back through my old code and see tons of '%s %d' % (x, y) bullshit and wonder how I could have ever been such a uneducated swine.
→ More replies (2)
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
u/Pragalbhv Aug 26 '20
Flair up and join the python gang!
49
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 (4)13
u/Nimeroni Aug 26 '20
If there was a python that compiled to native executables, everything in the universe would be gucci
Well, you can compile Python into C (with Cython), then compile the C into a native exe.
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.
4
Aug 26 '20
Bro, ive been coding for 4 years, 3 of them i only used .net, im not the bet person to answer, but i currently prefer java
5
u/lacks_imagination Aug 26 '20
Can I ask why? Some people tell me that they don’t particularly like Java but they use it because it is perfect for app design.
→ More replies (3)→ More replies (7)3
u/raltyinferno Aug 26 '20 edited Aug 26 '20
What platform do you want to be designing them for? My current job has had me sinking into .NET and I gotta say, it's pretty amazing.
C# is basically Java but better, and the ecosystem around it has so many cool tools.
But, a lot of the cooler parts of it are aimed at windows or web development, so if you want to be developing for mac/Linux other options might be better.
If you mean designing mobile apps for Android and iOS, then checkout Xamarin. It's a .NET framework for writing a single cross-platform app that runs on both. https://dotnet.microsoft.com/apps/xamarin
→ 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
5
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)")0
35
u/treenaks Aug 26 '20
#! perl
print "B", "r" x 30, $/;7
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
27
15
u/ProbablyInnacurate Aug 26 '20
Oh yeah, defs. I get stuck making them complex, unreadable and un-pythonic because it's fun.
7
u/Ghos3t Aug 26 '20
I've heard list comprehension is more optimized that map, reduce etc due to the way it is implemented in Python. Something to do work map, reduce being function calls
→ More replies (3)6
Aug 26 '20
My favorite is that there are options so you can pick the syntax you prefer
→ More replies (1)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)→ More replies (3)7
u/Rasmaellin Aug 26 '20 edited Jan 01 '21
Learn Haskell! We have general monad comprehensions. Lists,
IO, parser combinators—as long as it's a monad, we have comprehensions for it. And sometimes even comprehensions for non-monads withApplicativeDo.Heck, I wrote a brainfuck interpreter comprehension not too long ago.
→ More replies (3)→ More replies (139)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.
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.
7
u/renrutal Aug 26 '20
I can see the schemers and snake people sighing in relief dodging that bullet.
→ More replies (1)0
→ 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 (11)42
Aug 26 '20 edited Aug 26 '20
Why does that not make sense? Adding an object to a string implicitly converts the object to a string and then concatenates the two strings, and the default conversion for object is
"[object Object]"if.toString()isn't defined.Next you're going to tell me that
5 + 1.0should also error because it implicitly promotes an integer to a double.Edit: so this comment is dragging out all of the butthurt python fanbois. Here's the deal: your pretty little scripting language was written with auxiliary operating system tasks in mind, and was later adopted by statisticians and mathematicians. Of course it has hard typing and extensive error handling.
But JavaScript was originally integrated into HTML. That's frontend. Frontend needs implicit string conversions, because typing
str(some_var)over and over again gets real annoying, real fast."10" == 10is a bit more arguable, but I suppose it has its use in validating input. Also, when you have a user on your frontend, the last thing you want is everything to come crashing down because of some formatting error in a string that gets shown to the user maybe once in a blue moon. There's probably some performance reasons for the way things are as well, because V8 compiles hot code into machine code - I imagine it's cheaper to just have a toString() defined to return a constant somewhere instead of checking for nullptr and slinging errors around...In any case, Lua is, objectively, the best scripting language.
→ More replies (40)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.
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
→ More replies (31)6
u/Alikont Aug 26 '20
Maybe because you have a choice to not use Python. People who hate it just don't use it.
You don't have a choice to not use JS because it has monopoly on browsers.
→ More replies (2)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
u/YellowBunnyReddit Aug 26 '20
Okay, but why are you suddenly talking about C++?
5
u/AceOfShades_ Aug 26 '20
I may have misread the single letter ‘C’. So that doesn’t bode well for my productivity today.
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)2
u/Sarcastinator Aug 26 '20
C# supports this as well though the syntax is different.
list[^1]→ More replies (2)0
0
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"0
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.
3.0k
u/epiquinnz Aug 26 '20
I love how this ancap zoomer is always so opinionated on technical programming stuff.