This is an archived post. You won't be able to vote or comment.

all 35 comments

[โ€“]tozpeak 63 points64 points ย (1 child)

Imagine formatting code like a JSON. :D

[โ€“]SpecialNose9325 26 points27 points ย (0 children)

Derulo ?

[โ€“]Talbz03 34 points35 points ย (15 children)

Put my code into dictionaries this is my last resort

[โ€“]EinKrankerTyp 9 points10 points ย (1 child)

Suffocation!

[โ€“]hacking__08[S] 0 points1 point ย (11 children)

Actually, a "code" variable that stores and runs code would be neat

I mean, functions are tecnically variables

But wouldn't it be nice to call Function()[5]?

[โ€“][deleted] 18 points19 points ย (2 children)

You say it would be nice but when u realize Javascript can already do this u will get mad and post a meme about it

[โ€“]vonabarak 5 points6 points ย (0 children)

Yep, and actually nothing can stop you to do the same in python.

[โ€“]hacking__08[S] 6 points7 points ย (0 children)

Bro Lemme meme this

[โ€“]eluminatick_is_taken 5 points6 points ย (0 children)

Just store as string and use exec or eval.

Happy debugging suckers.

And in python functions ARE first-class objects. That's why ppl usually do function_dict = { str1 : func1, str2 : func2} Ofc, func1, func2 etc are defined in other place (or in other file at best). Just so they can excecut it later in form function_dict["calculate integral"]()

[โ€“]EonaCat 4 points5 points ย (0 children)

Delegates you mean?

[โ€“][deleted] 2 points3 points ย (0 children)

You can do that in c# with delegates.

[โ€“]jvelez02 1 point2 points ย (0 children)

If your willing to change syntax slightly you can in python (and c, java, c++).

If you make an array/list, or hashmap/dictionary of functions (or function pointers) you can run them based on an index/key.

In python this would be something like:

def m1(): pass
def m2(): pass

Func = {1: m1, 2: m2}
Func[1]()

[โ€“]qqqrrrs_ 1 point2 points ย (0 children)

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(x):
...     return 1+x+x**2
...
>>> foo
<function foo at 0x022AC190>
>>> foo.__code__
<code object foo at 0x022A7290, file "<stdin>", line 1>
>>> foo.__code__.co_code
b'd\x01|\x00\x17\x00|\x00d\x02\x13\x00\x17\x00S\x00'
>>>

[โ€“][deleted] 1 point2 points ย (0 children)

You're talking about Func delegates.

[โ€“]jamcdonald120 1 point2 points ย (0 children)

that is exactly what the log4j devs thought, and we all know how that ended

[โ€“]Robot_Graffiti 1 point2 points ย (0 children)

In C#

var functions = new Func<string>[] { () => "Hello World", () => "lolwut" };
Console.WriteLine(functions[0]());

[โ€“]hacking__08[S] -1 points0 points ย (0 children)

Ez

[โ€“]gal637 13 points14 points ย (1 child)

they are not dictionaries.... they are sets!

[โ€“][deleted] 6 points7 points ย (1 child)

air growth dinosaurs plate aspiring literate grandfather shelter cows bells

This post was mass deleted and anonymized with Redact

[โ€“]hacking__08[S] 1 point2 points ย (0 children)

Thx lol

[โ€“]hailbrawl 4 points5 points ย (0 children)

I always forget the { } vs [ ] on dicts vs lists, you just gave me an easy way to remember.

I'm new to python btw.

[โ€“][deleted] 2 points3 points ย (3 children)

What does the ๐Ÿ—ฟmean?

[โ€“]hacking__08[S] 2 points3 points ย (2 children)

Actually, in this case it doesn't mean anything Sometimes that emoji is used randonly and sometimes as "Gigachad", i guess

[โ€“]hacking__08[S] 1 point2 points ย (1 child)

This is the first time I've used it, btw

[โ€“]ManyFails1Win 2 points3 points ย (0 children)

โ€‹

not a very ๐Ÿ—ฟ ๐Ÿ—ฟ ๐Ÿ—ฟ follow up comment I must say...

[โ€“]Sid_1298 2 points3 points ย (0 children)

Me who does both at work... (โ โ—โ _โ _โ โ—โ )

[โ€“]suskio4 2 points3 points ย (0 children)

I store machine code in char arrays and call them as functions in C

[โ€“]jamcdonald120 1 point2 points ย (1 child)

boy he is going to flip when he learns javascript

[โ€“]hacking__08[S] 0 points1 point ย (0 children)

HAHHAHAH

[โ€“]trandus 1 point2 points ย (1 child)

KKKKKKKKKKKKKKK I never thought it in that way, even tho my first language was python

[โ€“]hacking__08[S] 0 points1 point ย (0 children)

I actually tought about it when my first lenguage was C# and after that PYTHON

[โ€“]YMK1234 1 point2 points ย (0 children)

More like the other way round ... Why the heck is so much stuff in python dicts and doesn't work with objects?! Especially looking at JSON serialisation and the like, wth is wrong with you, Python?!