LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2017, 12:00 PM   #1
svetlanarosemond
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Rep: Reputation: Disabled
Funny things Java Devs have to do, but Python devs don't


I posted a question about abstract class and if I correctly constructed one and used it properly on another forum.

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

For example, suppose I have the following Weapon class:

Code:
from abc import ABCMeta
from abc import abstractmethod
 
 
class Weapon(metaclass=ABCMeta):
 
    def __init__(self, name, damage):
        self.name = name
        self.damage = damage
 
    @abstractmethod
    def prepare(self):
        pass
 
    @abstractmethod
    def attack(self):
        pass
 
    @abstractmethod
    def cleanup(self):
        pass
 
    def __str__(self):
        return "Name: {} Damage: {}".format(self.name, self.damage)
and implementation:

Code:
from Weapon import Weapon
 
 
class ChargeGun(Weapon):
 
    def __init__(self, name, damage):
        super().__init__(name, damage)
 
    def prepare(self):
        print("Aiming my Gun");
 
    def attack(self):
        print("Shooting")
 
    def cleanup(self):
        print("Lowering gun, and turning off laser sight")
         
    def charge(self):
        print("Charging Gun. Please hold..(elevator music playing)")

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.

Example:

Code:
interface Weapon{
    int attack();   
}


public class ChargeGun implements Weapon {
    //Constructor, and Weapon interface implementation  
    //...
    public void charge(){}
}

in Main:

//Can't access charge method without downcasting which means I have to use getClass() or instanceof
Weapon chargeGun = new ChargeGun();


The Work around is the visitor pattern:

class WeaponVisitor {
   void visit(ChargeGun aGun) { }
}

interface Weapon {
  ...
  void accept(Visitor v);
}


class WeaponVisitorImpl extends WeaponVisitor {
     @Override void visit(ChargeGun aGun) {
      System.out.println("Charging Gun");
      aGun.charge();
     }
 }


Main:

Weapon chargeGun = new ChargeGun();
WeaponVisitorImpl wvi = new WeaponVisitorImpl()
wvi.visit(chargeGun);


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

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

Any stories of specific tasks made easier with Python than another language?

Last edited by svetlanarosemond; 11-02-2017 at 04:39 PM.
 
Old 11-02-2017, 03:50 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
I don't get it. Could you post the Java code for comparison?
 
Old 11-02-2017, 04:36 PM   #3
svetlanarosemond
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
I don't get it. Could you post the Java code for comparison?
See update
 
Old 11-02-2017, 05:42 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by svetlanarosemond View Post

Main:
Code:
Weapon chargeGun = new ChargeGun();
WeaponVisitorImpl wvi = new WeaponVisitorImpl()
wvi.visit(chargeGun);
Or just

Code:
ChargeGun chargeGun = new ChargeGun();
chargeGun.charge();
 
Old 11-03-2017, 06:58 AM   #5
svetlanarosemond
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Code:
ChargeGun chargeGun = new ChargeGun();
chargeGun.charge();
You can do this, but in Java/C# it's best to think in abstract terms, so that I don't have to keep a separate collection for game guns. In Python, it's better I declare and use the subclass directly, no need to say:

Code:
Weapon chargeGun = new ChargeGun();
charge.attack();
I can still keep a list of Weapons, any weapon that doesn't implement charge throws an exception.


In Python I just do:

Code:
ch = ChargeGun();
Code:
ch.charge()
and I have access to the sub class specific methods. No need for messy down casting, no need for extra classes to do the work. Python presented a clean, minimal, solution. My whole point is, what Python has made easy, Java, and C# have made complicated.

Last edited by svetlanarosemond; 11-03-2017 at 06:59 AM.
 
Old 11-03-2017, 09:19 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
The worst thing that Java developers have to do ... is to put up with(!) Java!

It is a most exasperating language . . .
 
Old 11-03-2017, 10:18 AM   #7
svetlanarosemond
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
The worst thing that Java developers have to do ... is to put up with(!) Java!

It is a most exasperating language . . .
I know!

The most trivial tasks are hindered by the dumbest things. For example, out-of-box, no imports necessary, I can use python's list,dictionary, and tuples, simply by declaring it. In Java, and I'm not making this up, If I wanted to use a List, I have to import two collections, because List is a generic type, I have to use it with a more concrete implementation, example, ArrayList. This means I have to write two import statements, and then write my code to use the List/ArrayList. Another example, simple use of the with keyword to read and write to files, ensures that the resources are cleaned up afterwards. Just imagine the Java code, try-with-resources and a class that declares a buffer reader that take a file path as the parameter.
 
Old 11-03-2017, 10:22 AM   #8
svetlanarosemond
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
The most trivial tasks are hindered by the dumbest things. For example, out-of-box, no imports necessary, I can use python's list,dictionary, and tuples, simply by declaring it. In Java, and I'm not making this up, If I wanted to use a List, I have to import two collections, because List is a generic type, I have to use it with a more concrete implementation, example, ArrayList. This means I have to write two import statements, and then write my code to use the List/ArrayList.

Using and IDE some what fixes this, as the IDE will complain, but if I were using Notepad++ and forgot, then I'm have to deal with it. I can use Python in notepad++ and not worry about importing.

Another example, simple use of the with keyword to read and write to files, ensures that the resources are cleaned up afterwards. Just imagine the Java code, try-with-resources and a class that declares a buffer reader that take a file path as the parameter.

Python FTW!
 
Old 11-03-2017, 09:36 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by svetlanarosemond View Post
Code:
ChargeGun chargeGun = new ChargeGun();
chargeGun.charge();
You can do this, but in Java/C# it's best to think in abstract terms,
Java is fairly clunky anyway, but it sounds like you're adding some extra clunkiness on top in the name of Object Orientedness. Think in abstract terms when it helps, don't when it doesn't.
 
1 members found this post helpful.
Old 11-04-2017, 05:19 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@OP: if you are a Python-advocate, you could help with this problem: https://stackoverflow.com/questions/...colon/46554601
 
Old 11-04-2017, 08:29 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by NevemTeve View Post
@OP: if you are a Python-advocate, you could help with this problem: https://stackoverflow.com/questions/...colon/46554601
From the answer that is already there, it sounds like it's just a bug in the python interpreter on a particular OS/version.
 
Old 11-04-2017, 08:43 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Sure; I think it would require a Python-enthusiast to find the bug. (I myself did try, but could not reproduce the problem (with AIX and gcc), though I did find a 600-line long function called _Py_dg_dtoa that I suspect to be the culprit; it is full with single-letter variable-names, goto's into blocks and other 'advanced features'.)
 
Old 11-04-2017, 10:05 AM   #13
svetlanarosemond
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Java is fairly clunky anyway, but it sounds like you're adding some extra clunkiness on top in the name of Object Orientedness. Think in abstract terms when it helps, don't when it doesn't.
Thanks for the feedback!

Could you just explain a little more, when you say Think in abstract terms when it helps, don't when it doesn't., meaning, don't just have a subclass specific method just to have one? In my case, charge could have been in the prepare method, correct? Or are you saying I don't need classes at all?
 
Old 11-04-2017, 11:01 AM   #14
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by svetlanarosemond View Post
Could you just explain a little more, when you say Think in abstract terms when it helps, don't when it doesn't., meaning, don't just have a subclass specific method just to have one? In my case, charge could have been in the prepare method, correct? Or are you saying I don't need classes at all?
It's hard to say, because your example code is kind of vague and contrived (making a realistic example short enough to fit in a post can be pretty difficult though). I just see that you say a simplification is not a good idea because it's "best to think in abstract terms". But if "thinking in abtract terms" is giving you more complicated code, I would say it's not best.

To put in terms of your example: In your python program you don't check that the charge() function is present. So it must be that you know you have a ChargeGun object. The correct way to encode this knowledge in Java is to give the variable the ChargeGun type. Giving it the more abstract Weapon type means you throw away the knowledge, hence you need more complication to recover it.

Quote:
Originally Posted by NevemTeve View Post
Sure; I think it would require a Python-enthusiast to find the bug.
A Python-enthusiast who is also an AIX enthusiast, and has a good knowledge of C and also the internal implementation of the Python interpreter, you mean? Somehow, I suspect there aren't many people who fit all these criteria...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Sick of Java and C++? Google pours a cup o' Kotlin for Android devs LXer Syndicated Linux News 0 05-19-2017 01:41 AM
LXer: Krita Devs Work on SVG Support, Python Scripting, and Text Tools for Krita 3.2 LXer Syndicated Linux News 0 12-23-2016 05:07 AM
LXer: Devs to pour Java into Amazon's cloud after AWS Lambda update LXer Syndicated Linux News 1 06-16-2015 08:08 PM
LXer: Red Hat ships piping hot Ceylon to curry favor with Java-weary devs LXer Syndicated Linux News 0 11-14-2013 01:30 PM
Don't let the gnome devs do this PatrickMay16 General 10 10-21-2007 12:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration