OO Programming

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1608 Views - Last Post: 04 August 2012 - 09:00 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

OO Programming

Posted 03 August 2012 - 10:19 AM

When programming in Python, do you force it to use OO constructs like classes etc. Let's say you were to implement a Genectic Algorithm. Would you make a Population class and encapsulate all the mutations and crossovers inside, or would you just make basic functions like: initPopulation() , crossover() muatate() etc.

I am asking, because coming from Java that forces you to classes I dont know how i should approach it. I never had that freedom of not having to put everything inside a class.

Thanks.

This post has been edited by atraub: 05 August 2012 - 03:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: OO Programming

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8000
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: OO Programming

Posted 03 August 2012 - 10:23 AM

If it makes sense to put it in a class, put it in a class. If it's a persistent object that needs to maintain state and has particular ways in which that state is to be manipulated, make it a class.

If it doesn't need the advantages of classes, don't use classes.
Was This Post Helpful? 2
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: OO Programming

Posted 03 August 2012 - 10:26 AM

When doing Python, you typically do what is "Pythonic". I know that is rather vague for a non-Python guy (I'm also a Java and PHP guy, mind you). What I like to do is to make my data structures and large parts of the code that are separate but similar in functionality a class. But things that would normally be static in Java, I make functions in Python. Make sure to use lists and generators as best as you can. Basically, in Python, small, readable code is key.

I would start with non-classes and slowly add them in as you see fit, since you are a Java person.
Was This Post Helpful? 2
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: OO Programming

Posted 03 August 2012 - 10:30 AM

Thank you.
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: OO Programming

Posted 03 August 2012 - 11:23 AM

Nail on the head guys. While Java forces you to create classes and whatnot for the simplest of programs, Python does not. If you want to do a hello world program in Java, your code would be
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

(Taken from docs.oracle.com)

In Python, you don't need that stuff, your program would literally be:
print("Hello World")


That being said, you can use object orientation for Python if that makes your problem easier to solve, or makes your code more maintainable, or any other reason you might use OO. That's one of the major benefits of Python IMO. Use what you need when you need it!

This post has been edited by atraub: 03 August 2012 - 11:24 AM

Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8000
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: OO Programming

Posted 03 August 2012 - 11:39 AM

View Postatraub, on 03 August 2012 - 01:23 PM, said:

Nail on the head guys. While Java forces you to create classes and whatnot for the simplest of programs, Python does not. If you want to do a hello world program in Java, your code would be
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

(Taken from docs.oracle.com)

In Python, you don't need that stuff, your program would literally be:
print("Hello World")


That being said, you can use object orientation for Python if that makes your problem easier to solve, or makes your code more maintainable, or any other reason you might use OO. That's one of the major benefits of Python IMO. Use what you need when you need it!



Just out of curiosity, does anyone really care about the class and method declarations in Java? I mean, there's plenty to complain about in Java, and a lot to like as well, but the class and method declarations? It never seemed to me worth making a fuss over, and it's always cited as the big pain by python people. What's the deal?

Not trying to start a language war, I use java and I like it, and I'm learning python and I like that too. I'm just curious about why I keep seeing this one trivial point coming up.
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: OO Programming

Posted 03 August 2012 - 12:42 PM

I try not to speak for others, but I believe some people view it as a big steaming pile of syntactic sugar that shouldn't be necessary if you're just trying to write a quick script.

It's been years since I've used Java, but I seem to recall thinking that simply getting user input from a terminal and outputting some info was more annoying that it should be.

EDIT:
A quick google search illustrates my point. The first result of "Java get user input" yields
import java.io.*;

public class GetUserInput {
    public static void main (String[] args) {
       System.out.print("Enter your name and press Enter: ");
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String name = null;
       try {
         name = br.readLine();
       } catch (IOException e) {
         System.out.println("Error!");
         System.exit(1);
       }
       System.out.println("Your name is " + name);
}
}


We pythoneers prefer:
name = input("Enter your name and press Enter: ")



EDIT 1.5:
Ok, this site has a simpler example for just getting basic user input, but it's still a bit more "stuff" than I think should be necessary.

EDIT 2:
This may sound like pandering, but I am not so blinded by py-love that I can't see benefits to Java. IMO, making a clean UI in Java is far easier than in Python (and I don't mean netbeans :-P).

This post has been edited by atraub: 03 August 2012 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8000
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: OO Programming

Posted 03 August 2012 - 12:52 PM

Two lines makes a heap? Hm. I guess

#!/usr/bin/python


is half a heap?
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: OO Programming

Posted 03 August 2012 - 12:56 PM

System.out.println("Enter your name and press Enter: ");
String name = new Scanner(System.in).nextLine();


Doesn't seem too bad to me.
Was This Post Helpful? 1
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: OO Programming

Posted 03 August 2012 - 01:02 PM

haha I mainly use windows, so I usually don't think about the shebang tag, let alone include it. That's really more a *nix thing rather than a python thing though.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8000
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: OO Programming

Posted 03 August 2012 - 01:04 PM

View Postatraub, on 03 August 2012 - 02:42 PM, said:

A quick google search illustrates my point. The first result of "Java get user input" yields
(snipped)


your example is a little tortuous. More ordinary would be

Scanner input = new Scanner(System.in);
System.out.println("Enter your name");
String name = input.nextLine();


Since in Java we don't usually write programs that just get input and throw it away - usually you're doing something with it - the overhead is amortized into nothing. If you're actually writing a program that does something, that stuff disappears into the background and the structure it provides is actually useful.

Of course, if I'm doing console input I'll generally make a quick method which calls exactly like the python one, but has the particular restrictions that I want - for example

int i = query("How many lumps of sugar would you like?", 1);


where the 1 is a token of the type I expect in response (usually I have one for int and one for string, since I seldom need floating-point input). That query method will then handle stuff like type checking and looping until the user gets it right, and it lives in the background.
Was This Post Helpful? 0
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: OO Programming

Posted 03 August 2012 - 01:04 PM

View PostRyano121, on 03 August 2012 - 03:56 PM, said:

System.out.println("Enter your name and press Enter: ");
String name = new Scanner(System.in).nextLine();


Doesn't seem too bad to me.

Haha I'm not trying to create a language war either. That being said, you forgot the class declaration, the method declaration, and the import statement for the scanner. Whereas
name = input("Enter your name")


runs on it's own in Python.

I'm not trying to say that Python is the greatest thing ever, only that it abstracts out a lot of the syntactic sugar of simple console applications.

View Postjon.kiparsky, on 03 August 2012 - 04:04 PM, said:

your example is a little tortuous.

I agree. I found a better example I would have used if I could have copied and pasted in it, but the author felt the need to include the code as a screen shot, but I did link it in edit 1.5.

This post has been edited by atraub: 03 August 2012 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: OO Programming

Posted 03 August 2012 - 01:05 PM

View PostRyano121, on 03 August 2012 - 03:56 PM, said:

System.out.println("Enter your name and press Enter: ");
String name = new Scanner(System.in).nextLine();


Doesn't seem too bad to me.


That won't run all by itself. The Python example will. Simply said, I find it easier to make LARGE systems and programs in Java and prefer Python for work needing to be done fast. Doesn't mean it can't be used the other way around....just the way I use it.
Was This Post Helpful? 1
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: OO Programming

Posted 03 August 2012 - 01:10 PM

View PostDogstopper, on 03 August 2012 - 04:05 PM, said:

I find it easier to make LARGE systems and programs in Java and prefer Python for work needing to be done fast. Doesn't mean it can't be used the other way around....just the way I use it.


This
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8000
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: OO Programming

Posted 03 August 2012 - 01:13 PM

View PostDogstopper, on 03 August 2012 - 03:05 PM, said:

View PostRyano121, on 03 August 2012 - 03:56 PM, said:

System.out.println("Enter your name and press Enter: ");
String name = new Scanner(System.in).nextLine();


Doesn't seem too bad to me.


That won't run all by itself.



Why would you want it to? It doesn't do anything.
I do agree with you that java is not what I'd go to for trivial cases. Python is definitely better suited to stuff you don't want to maintain. In that way it seems to be more of a post-perl than an anti-java.

EDIT:Okay, I admit I'm baiting a little here - but with a purpose. The complaints about the Java boilerplate, which are not unique to the pythrong, seem to me to reveal more of a hostility to Java as a language than an understanding of the real differences between the languages. For the record, I agree with what dogstopper actually meant, in addition to the parody of what he said.

This post has been edited by jon.kiparsky: 03 August 2012 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2