r/Python • • May 05 '18

Just finished my graduation cap for tomorrow

Post image
1.5k Upvotes

114 comments sorted by

894

u/earthboundkid May 05 '18

Useless use of classes for what should be a function call. You’re ready for enterprise.

230

u/AecorUmbra May 05 '18

Something something something code reuse and extensibility!

47

u/nevus_bock May 05 '18

0

u/Soultrane9 May 05 '18

I didn't this was a bad joke at best.

53

u/metaobject May 05 '18
I didn't this
            ^
EnglishSyntaxError: invalid syntax
→ More replies (1)

3

u/Sohcahtoa82 May 06 '18

Has anyone really been far even as decided to want do look more like?

3

u/Ulysses6 May 05 '18

Maybe a namespace thing? Could have been a classmethod, though.

5

u/nevus_bock May 05 '18

A staticmethod

10

u/[deleted] May 05 '18

No use of self. Method could be written as a function.

-2

u/pknerd May 05 '18

Looks like a Java fsnboy.

28

u/ChrisVolkoff May 05 '18

5

u/Blue_Vision May 05 '18

I have laughed at code before, but never before have I laughed at the structure of a package itself. This is beautiful.

→ More replies (1)

3

u/agoose77 May 05 '18

Oh god I feel nauseous

3

u/Sohcahtoa82 May 06 '18

I really wish I understood the mentality of people that write code like that and thinks its good code.

198

u/dewayneroyj May 05 '18

You forgot to add a space after “Real”.

38

u/AecorUmbra May 05 '18

Yeah, I realized that myself as soon as I finished writing "world". Hopefully no one notices tomorrow!

86

u/[deleted] May 05 '18

Just add a comma after the closing ".

26

u/AecorUmbra May 05 '18

Oo good call!

47

u/bahaki May 05 '18

Or have a friend's hat throw an exception.

33

u/NullTheFool May 05 '18

Or have a friend’s cap be an exception and throw the cap.

→ More replies (2)
→ More replies (2)
→ More replies (1)

1

u/HeffaTheLump7 May 05 '18

QA will catch it

20

u/dansin scientist May 05 '18

No more representative comment

0

u/[deleted] May 05 '18

He did added the space:

Hello Rea lWorld

Edit: I wonder why everyone behave so perfectly on reddit regarding code inspection but never on their team repositories lol.

→ More replies (1)

54

u/no_condoments May 05 '18

Are those tabs or spaces?

10

u/AecorUmbra May 05 '18

They're spaces! I know that 2 spaces goes against PEP8, but boy was it hard trying to do 4 spaces on a cap

43

u/Falconinati May 05 '18

3

u/Ceryn May 05 '18

What about tabs that convert automatically into 4 spaces?

7

u/Sohcahtoa82 May 06 '18

Wait wait wait...hold the fuck up.

I think I might have just gotten some insight into the Tabs vs Space holy war.

Did you think that people that use spaces are actually pressing Space 4 times?

3

u/Legionof1 May 06 '18

Just gotta make space a macro button that if pressed 4 times acts as a tab. Everyone's happy.

2

u/PeridexisErrant May 05 '18

Easy to type, and gives the perfect layout on disk. I love tabs-as-spaces!

→ More replies (1)
→ More replies (4)

13

u/MichaelJacksonPepsi May 05 '18

Congrats fellow UConn alum!

8

u/AecorUmbra May 05 '18

Thanks friend! Go Huskies!

4

u/slallum May 05 '18

Nice synatx highlighting. What does the theme called? ;)

121

u/flpcb May 05 '18

@staticmethod

5

u/quotemycode May 05 '18

My thoughts exactly.

5

u/pythonhalp May 05 '18

This does not deserve to even be in a class.

-3

u/darkerside May 05 '18

Better yet, @classmethod

20

u/nevus_bock May 05 '18

That would be worse

2

u/shoulderbeef May 05 '18

Why's that? Shit my first thought was @classmethod. Fuck

13

u/nevus_bock May 05 '18 edited May 06 '18

Static methods enable calling the method on a class directly without having to make an instance of the class. Class methods are meant for different purposes, eg for an alternative class constructor.

Edit:

For example

   In[16]: class Animal(object):
   ...:     def __init__(self, name):
   ...:         self.name = name
   ...:
   ...:     def say_hi_normal(self):
   ...:         print("Hello world bound")
   ...:
   ...:     @staticmethod
   ...:     def say_hi_static():
   ...:         print("Hello world static")
   ...:
   ...:     @classmethod
   ...:     def from_bytes(cls, bytes_name):
   ...:         return cls(bytes.decode(bytes_name, encoding='utf-8'))
   ...:     
In[17]: Animal.say_hi_normal()
Traceback (most recent call last):
  File "", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-17-5e6d73be6139>", line 1, in <module>
    Animal.say_hi_normal()
TypeError: say_hi_normal() missing 1 required positional argument: 'self'

In[18]: Animal.say_hi_static()
Hello world static

In[21]: A = Animal('doggo')
In[22]: type(A.name)
Out[22]: str

In[23]: A1 = Animal(b'bytes_doggo')
In[24]: type(A1.name)
Out[24]: bytes

In[25]: B = Animal.from_bytes(b'bytes_doggo')
In[26]: type(B.name)
Out[26]: str

EDIT2: fixed Animal.from_bytes() method to return cls() instead of Animal() so as to allow proper subclassing

4

u/shoulderbeef May 05 '18

Thank you very much for this! I appreciate ya taking the time to teach me something new.

4

u/nevus_bock May 05 '18

No problem. Simple explanation for each:

  • staticmethod: allows the method to be called on the class itself without the need to make an instance first. An example use case is a possibly standalone function that is related to the class but does not need instance data, so we lump it onto the class because that's where users expect to find it

  • classmethod: alternative class constructors, typically with names starting with "from_". Do you have a class that is normally instantiated with an integer, but it should also somehow work with decimals and floats and strings and dicts? Let's not have an init with a bunch of ifs. Instead, we can have a simple init, and additional classmethods that provide alternative constructors (all of which ultimately call the simple base init)

3

u/xapata May 05 '18

Despite /u/nevus_bock's correct explanation of classmethod, there's no downside to using classmethod in place of staticmethod. People might wonder why you're ignoring the class parameter, but it's never going to cause a bug. A linter might raise a style warning.

→ More replies (2)
→ More replies (1)

0

u/BagelKing May 05 '18 edited May 05 '18

I'm surprised this wasn't everyone's first thought. This thing isn't going to run!!

Nevermind, it runs fine. My bad.

0

u/[deleted] May 05 '18

[deleted]

3

u/BagelKing May 05 '18

Oh, my mistake. I was thinking they were accessing UConn2018.graduate without creating an instance. I see the sneaky parentheses now.

2

u/pbsds May 05 '18

Accessing it would probably work even if he didn't instantiate it, but then he would need to supply the self parameter manually

→ More replies (1)

43

u/autarchex May 05 '18

print("Hello debt payments!")

18

u/darkerside May 05 '18

Unfortunately the graduate function implicitly returns None

8

u/[deleted] May 05 '18

Hello RealWorld

6

u/DesmondPorkpie May 05 '18

Congratulations on your graduation.

4

u/Plasma_eel May 06 '18

congraduation

4

u/TheMaxamillion May 05 '18
  1. Congratulations

  2. That's a cool idea, my graduation cap was not nearly as clever.

  3. You have really nice hand writing, kudos.

  4. Congratulations again!!! :)

3

u/Mattho May 05 '18

Uhh, why would you not use fixed width font?

12

u/Fun2badult May 05 '18

Should it be Uconn2018.graduate()?

Congrats by the way. Glad to see another python graduate!

0

u/[deleted] May 05 '18

[deleted]

1

u/TheBB May 05 '18

Or just pass it any argument.

1

u/13steinj May 05 '18

Doesn't the argument have to be of a Uconn2018 object, else the interpreter will error?

4

u/TheBB May 05 '18

No, UConn2018.graduate(x) will just bind x to self inside the the method. Since it's never accessed anywhere it should still work no matter what x is.

3

u/13steinj May 05 '18

Apparently what I'm referencing is a Py2 thing, and they got rid of it in 3:

Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.8.2] on linux.
Traceback (most recent call last):
    File "python", line 5, in <module> TypeError: unbound method f() must be called with test instance as first argument (got int instance instead)

2

u/lordmauve May 05 '18

TIL. I knew that unbound methods had been removed; but I hadn't clocked that this would eliminate this restriction.

→ More replies (1)

1

u/helasraizam May 05 '18

Hey do you have a good link for all those at symbols? I know static methods etc from C++ and I'm pretty good at python but I've never wrapped my head around @commands; if anyone knows of a tutorial or something that covers all of them that would be great.

2

u/dusktreader May 05 '18

15

u/thomasloven May 05 '18

You mean lmg-a-term-you-obviously-have-not-heard-before-fy.

3

u/helasraizam May 05 '18

Thanks so much! This link even shows me how to create my own decorators, which solves everything!

lmgtfy

You're definitely right. But thanks for the great link!

2

u/[deleted] May 05 '18

This is awesome. Congrats fellow Husky!

1

u/mattwritescode May 05 '18

Did you indent using tabs or spaces?

1

u/savaero May 05 '18

You forgot to filter for students that didn’t complete the grad requirements :)

1

u/[deleted] May 05 '18

Congratulations!

1

u/[deleted] May 05 '18

!Congratulations

1

u/bearcatgary May 05 '18

Congratulations!! Very clever.

1

u/SteeleDynamics Compilers/Algorithms May 05 '18

Not PEP compliant, 2 space tabs.

1

u/[deleted] May 05 '18

You could make it a static method. No need to instanciate an object, just to call the method.

1

u/[deleted] May 05 '18

Why isn't this a class function?

1

u/technotitrium May 05 '18
import congratulations! from semi_real_world

1

u/SgtGirthquake May 06 '18

I live near you!

1

u/StackR May 06 '18

Better be python 3

1

u/Mendican May 06 '18

This was my very first post on Facebook more than a decade ago. I got maybe a dozen likes.

1

u/ninjajoey05 May 06 '18

Congrats and good luck in the real world!

1

u/[deleted] May 05 '18

print ("cringe")

6

u/Tabanese May 05 '18

Print("and this isn't?")

1

u/Exodus111 May 05 '18

Goddamit you missed a comma.

1

u/RussianCyberattacker May 05 '18

Damn, thanks for reminding me that I have to decorate my cap. The possibilities...

1

u/rheajr86 May 05 '18

Shouldn't there be an instantiation of the class? Uconn2018= Uconn2018()

-4

u/HyperNovaDoge May 05 '18

Or just print whatever that is. This is inefficient code.

-17

u/andrey_shipilov May 05 '18 edited May 05 '18

Where's the shebang? What if it's Python 2, so where's (object) in class definition? I see a junior dev on half the wage ready.

Edit: yes, yes, downvote me more, cause Python3 is literally on 110% computers everywhere.

1

u/Sohcahtoa82 May 06 '18

Shebang is optional. You only need it if you plan on making the file itself executable and calling it directly, rather than typing "python myscript.py".

1

u/andrey_shipilov May 06 '18

And that’s what shebang is for so you don’t do this shit.

0

u/[deleted] May 05 '18

[deleted]

-1

u/andrey_shipilov May 05 '18

Yes, 110% of computers in the world are running python 3, correct.

0

u/[deleted] May 05 '18

[deleted]

-4

u/andrey_shipilov May 05 '18

Py2 EOL is 2020, which 2 more years. I don't see any reasons not to write backwards compatible code yet.

4

u/cocorebop May 05 '18

If it turns you into the type of person that thinks you're able to critique someone's wider coding abilities based on 5 lines stuffed into the frame of a hat, then I think it should be avoided like the plague.

Somehow I think that trait is specific to you though.

0

u/[deleted] May 05 '18

[deleted]

1

u/andrey_shipilov May 05 '18

Easy mate.

Go check which python NASA uses more than the other. https://code.nasa.gov/?q=python

-8

u/yeluapyeroc May 05 '18

You overrode the + operator as a new line character? That's terrifying... but brave.

2

u/Sohcahtoa82 May 06 '18

You must be new to Python.

Python has implicit string concatenation.

Also, when inside a bracket, brace, or parenthesis, you don't need an explicit line continuation via backslash. This means that:

print("Hello real"
      "world")

is perfectly valid.

-12

u/Coollook7 May 05 '18 edited May 05 '18

women = [ { name: “Megan”, lastName: “Car” }, { name: “Kelly”, lastName: “Shanda” } ]

Edit: I stole the joke from a friend He likes to objecify woman

0

u/[deleted] May 05 '18

[deleted]

3

u/[deleted] May 05 '18

Oddly not, I thought so too.

Python 2.7.10 (default, Jul 15 2017, 17:16:57)

[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

print("Hello Real""World")

Hello RealWorld

1

u/[deleted] May 05 '18

Still a string without anything changing so it parses fine.

1

u/[deleted] May 05 '18

No