Funny things Java Devs have to do, but Python devs don't
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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'.)
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?
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
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.