this post was submitted on 01 Oct 2023
476 points (96.7% liked)

Programmer Humor

32464 readers
518 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

The audacity to do such a thing…

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 69 points 1 year ago (4 children)

In python, 'eval()' is your friend.

/maliciouscompliance

[–] [email protected] 38 points 1 year ago (1 children)
[–] [email protected] 2 points 1 year ago* (last edited 1 year ago)

Oh god I hate you so much for this. It's beautiful that it's possible but I also want you to know you're instigating cybercrimes.

[–] [email protected] 14 points 1 year ago

Nah, the locals() or globals() object is much better for this.

[–] [email protected] 8 points 1 year ago (1 children)

In c, nothing and nobody is your friend.

Source: me

[–] [email protected] 4 points 1 year ago (2 children)
[–] [email protected] 4 points 1 year ago* (last edited 1 year ago)

# is your worst enemy despite being a copy of a good language

[–] [email protected] 4 points 1 year ago

Objectively...

[–] [email protected] 7 points 1 year ago

In Perl, eval can do similar things, but symbolic references are "better" (I'm fairly sure it's where PHP got the idea, and the syntax, from.) e.g.

$foo = "bar";
$$foo = "potatoes"; # $$foo = access the variable named in $foo, i.e. $bar
print $bar; # prints potatoes

Reading other responses, it seems like Python's globals object is not entirely dissimilar, especially if you know how Perl deals with symbolic references under the hood.

But just because you can doesn't mean you should. If you use strict; in Perl, it will fail to compile most of this nonsense. Use a hash / associative array / dictionary / whatever your language (natural and/or programming) calls them instead.

And I'm pretty sure that even without strict, local variables can't be accessed at all the symbolic way, which is probably for the best. (NB: local is a subtle thing in Perl. By "local" here, I mean the so-called my variables that aren't accessible outside their scope. local variables are actually localised globals. Enjoy that thought.)