Evil

A forum for general discussion of the Python programming language.

Evil

Postby micseydel » Thu Jan 09, 2014 2:30 am

I didn't want to post the following code in the thread I'm referring to, since I didn't want the OP there to think it's ok to do, but I wanted to post this for fun. To show how serious I am about how this code shouldn't be used: I used tabs instead of spaces.
Code: Select all
def run_immediately(f):
    'decorator to cause functions to be run right after being defined'
    f()
    return lambda: None

print 
"defining square_4()"
@run_immediately
def square_4
():
    print 4**2

print 
"defining print_name()"
@run_immediately
def print_name
():
    print "michael"

def main():
    print "calling both functions now..."
    square_4()
    print_name()

if __name__ == '__main__':
    main()

Terminal session
Code: Select all
$ python evil.py
defining square_4()
16
defining print_name()
michael
calling both functions now...

The main() function is entirely unnecessary, especially the function calls though. If you leave them out you don't need to return the lambda either. My point being here just that you could have a function be called immediately and even prevent it from doing anything at all useful later. You definitely don't need to say the name to invoke it. Obviously at some level it's being explicitly invoked, but for an arbitrarily named decorator you probably wouldn't expect this behavior.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1354
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Evil

Postby Mekire » Thu Jan 09, 2014 3:18 am

Pretty amusing...

...but more importantly. We have syntax highlighting now?! And why no python brush?

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Evil

Postby micseydel » Thu Jan 09, 2014 4:08 am

It's php syntax highlighting, which has always been there. Just no one uses it. I usually forget.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1354
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Evil

Postby Mekire » Thu Jan 09, 2014 5:34 am

Yeah, I was unaware we had the ability to do even that.

Back to your code, a similar (or rather essentially identical) trick is used by certain individuals who think that needing to type:
Code: Select all
if __name__ == "__main__":
is for whatever reason non-aesthetically pleasing.

https://pypi.python.org/pypi/automain
http://slowchop.com/2011/01/25/automain/

As I find:
Code: Select all
if __name__ == "__main__":
to be utterly unoffensive I think it is rather silly though.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Evil

Postby metulburr » Thu Jan 09, 2014 5:44 am

It would be nice to have a plugin for python syntax highlighting, and a favicon.ico, and a logo.
hint hint jkbbwr!

@micseydel
quite interesting...

@mekire
I think it is rather silly though.

you think automain is silly or the if condition?

I use
Code: Select all
if __name__ == "__main__":
quite often, but i normally mean it as testing code that will be eventually removed from my module.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Evil

Postby Mekire » Thu Jan 09, 2014 5:57 am

I think automain is silly; sorry if that was unclear.

I have no problem with:
Code: Select all
if __name__ == "__main__"
And indeed believe that it should remain in the final production version (of the main script of course). I do occasionally use it to test individual modules, but this doesn't work very well in a package structure (and in general I don't do nearly as much TDD or testing as I should).

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Evil

Postby metulburr » Thu Jan 09, 2014 6:02 am

i cant believe someone made that
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Evil

Postby micseydel » Thu Jan 09, 2014 7:20 am

If I actually save a script, I always use that trick. Because I always write functions, and any function worth anything at all can be tested as a standalone component. I do sometimes keep the main logic of certain programs in that block though, and not in a separate main() function, so that I can run the script with -i and then examine the global state of the script. Not something I do once I get past a certain point, but I've certainly been known to do it.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1354
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Evil

Postby bunburya » Tue Jan 14, 2014 7:39 pm

The automain thing seems exceedingly silly, I mean it adds more clutter to your program than it saves.
http://www.bunburya.eu | https://github.com/bunburya

Join the #python-forum IRC channel on irc.freenode.net!
bunburya
 
Posts: 9
Joined: Sat Feb 09, 2013 7:28 pm
Location: Dublin, Ireland


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest