Python Forum
Funny things Java Devs do but Python Devs Don't
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Funny things Java Devs do but Python Devs Don't
#1
I've posted a question about abstract class and if I correctly constructed one and used it properly.

It made me remember all the funny procedures that Java devs have to go though for the simplest tasks.

For example, if I wanted to access the def charge(self): I just call cg.charge(), done!

In Java, it's another story.

I would need to down cast, which means using the instanceof keyword or getClass(), both of which is a big NO in Java. The other method is the visitor pattern. It's essentially just another class that takes the subclass and calls the method on that subclass. This has drawback of course, as you would have to know your sub classes a head of time or you would have to change the implementing class to accept that new type.

A lot of code for the simple task of accessing a subclass specific method.

Some days I just wake up, breathe in the fresh air, and think, damn it's good to work with Python. Smile

I'v got another example, but I'll share it later.

Any stories of specific tasks made easier with Python than another language?
Reply
#2
Generators make a lot of things very easy in python that would be much harder in a lot of other languages.  Consider a simple counter:
def counter(start_value=0, increment_by):
    value = start_value
    while True:
        yield value
        value += increment_by
In another language, such as c# or java, the easiest way to do that would probably be creating a whole class for it, to maintain state, and have a next_value method.  That said, I think those languages have been adding yield features, so it may not be as true as it once was.  Newer versions of javascript, for example, would look very similar to the python above (except with braces, obviously).  But then you run the issue of your code sometimes not working, as yield was added in Firefox in 2013, but has never worked in IE (reference: http://caniuse.com/#feat=es6-generators).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Setup of VSCode to run with Python: some things are messed up apollo 0 1,374 Jan-05-2022, 12:22 PM
Last Post: apollo
  a funny code nzcan 0 2,110 Oct-31-2018, 08:51 PM
Last Post: nzcan
  things i am working on Skaperen 0 1,838 Jul-24-2018, 07:46 PM
Last Post: Skaperen
  what to order to get started with Odroid HC 2 NAS things apollo 1 2,382 Jul-02-2018, 07:57 PM
Last Post: Larz60+
  Funny English Kebap 41 24,303 Feb-10-2018, 02:06 AM
Last Post: Skaperen
  a funny coding challege Skaperen 2 3,106 Dec-07-2017, 02:26 AM
Last Post: Skaperen
  Python almost as verbose as Java Ofnuts 5 5,190 Dec-05-2016, 09:53 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020