Do you dodge Python OO highlights, inclining toward the procedural/useful model? That propensity is basic in dialects, for example, PHP, where numerous software engineers pick not to utilize OO highlights. Yet, you may be passing up on a significant chance! Java requires an OO approach, giving you the benefits of that model in each line of code you compose. Stephen B. Morris brings up the upsides of utilizing Python’s OO highlights in a way like Java’s.
Framework: Object Orientation
Software engineers are animals of propensity, and we will in general stick with set up language highlights except if we make them urge motivation to grasp new ones. Article situated (OO) highlights are a genuine case of this issue. PHP software engineers consider PHP’s OO highlights to be a smart thought—however use them sparingly, if by any means. The story is like numerous Python developers, who favor not to utilize Python’s OO highlights.
Java sits at the opposite finish of the language range: It’s an OO language, so there’s no escaping from classes when you use Java. In spite of Java’s OO family, nonetheless, a ton of Java code is as yet written in a procedural way.
Why this predisposition against (or conceivable abuse of) OO? I think it comes down to a blend of individual tendency and designing judgment. On the off chance that a PHP or Python software engineer has broad involvement in one of these dialects and hasn’t utilized the OO includes frequently, the hesitance might be because of basic inactivity. Be that as it may, certain improvement errands may be better actualized in an OO setting than in the recognizable procedural/useful worldview.
The facts demonstrate that OO programming can bring about issues, for example, pile discontinuity or other nondeterministic stage states, for example, execution crumbling. To be sure, the issue of OO load utilize was one motivation behind why C++ took numerous years to supplant C in installed improvement ventures. Harking back to the 1990s, circle, CPU, and memory were at such a top notch, that, in any event in the psyches of architects, they blocked the utilization of OO dialects (which likewise blocked potential profitability gains from utilizing these emerging dialects).
I believe it’s despite everything reasonable for state that numerous Python developers evade OO highlights except if no other choice exists. In this article, I contrast Python and Java with show how they stack facing each other regarding multifaceted nature and speed. I trust this will take into account a goal evaluation!
TIP
Let’s take a look at some code, starting with Python.
A Python Class
As is the case with Python in general, the Python OO paradigm is pretty concise, as the simple class in Listing 1 illustrates.
Listing 1—A Python class.
class Student:def __init__(self, name, age, major):self.name = nameself.age = ageself.major = majordef is_old(self):return self.age > 100
The Student class has three data members: name, age, and major subject.
The __init__() method is the closest thing Python has to a constructor. Notice the use of self.nameto initialize the state of the instance. Also included is the simple method is_old() to determine (in a slightly “ageist” manner) whether the underlying student is young or old (with “old” being over 100 years).
The code in Listing 1 illustrates one of the great merits of OO programming: Code and data reside in close proximity to each other. Data is of course the repository of state, so the use of OO brings code, data, and state together in a manner useful to programmers. Clearly, you can do all of this without OO code, but OO makes it a matter of rather beautiful simplicity.
Remember: Most source code on the planet exists to model some real-world entity or process. OO can be a very clear, minimum-impedance technique for such modeling. This might even be a compelling reason for using the OO approach at all costs!
An Equivalent Java Class
Not to be outdone by our Python coding effort, Listing 2 shows an equivalent Java class.
Listing 2—A Java student class.
public class Student {String name;int age;String major;public Student() {// TODO Auto-generated constructor stub}public Student(String name, int age, String major) {this.name = name;this.age = age;this.major = major;} }
The Java code in Listing 2 is very similar to the Python code in Listing 1. Notice that the use of OO can produce quite readable code in either language. Listing 1 is not likely to baffle a Java programmer, even without a background in Python. Likewise, a Python programmer well versed in the Python OO features would easily understand the Java code in Listing 2.
So here’s our first takeaway: Well-written OO code can help to promote inter-language comprehensibility.
Why is this important? In our multi-language era, such comprehensibility is a prize worth pursuing. (For more on this topic, interested readers can check out my blog and my most recent eBook.)
The modern era of software can be defined by the rapid adoption of application deployment on the Web and the concomitant use of browsers to access those applications. Users now routinely demand from web-hosted applications what used to be called “desktop features.” Such usability generally can’t be delivered using just one programming language. Programmers must increasingly be comfortable in numerous languages: Java, Scala, JavaScript, HTML, CSS, Python, SQL, and so on.
A Matter of Speed: Python Versus Java Code
Speed is always an issue. Let’s modify Listing 1 so that we can get a feel for the speed of the underlying code.
Running the Python Code
Listing 3 illustrates a simple (toy) program that attempts to “stress” the platform a little.
Listing 3—A timed program run.
import timeclass Student:def __init__(self, name, age, major):self.name = nameself.age = ageself.major = majordef is_old(self):return self.age > 100start = time.clock()for x in xrange(500000):s = Student('John', 23, 'Physics')print 'Student %s is %s years old and is studying %s' %(s.name, s.age, s.major)print 'Student is old? %d ' %(s.is_old())stop = time.clock() print stop - start
Listing 3 is a slightly augmented version of Listing 1. This revised code does the following:
- Import the time module.
- Create a time snapshot at the beginning of the program.
- Instantiate a large number of Student objects.
- Access the data inside each object.
- Take a time snapshot and subtract the original time.
- Display the time required to run the program.
Admittedly, this is a pretty crude test. But let’s see an example run that creates 500,000 objects. This is an excerpt from the full program run:
Student John is 23 years old and is studying Physics Student is old? 0 29.8887370933
We can think of this as a baseline test: It takes about 30 seconds for a program run of 500,000 objects. Now let’s raise the number of objects created to 800,000:
Student John is 23 years old and is studying Physics Student is old? 0 48.2298926572
From this, we see that a program run of 800,000 objects takes about 48 seconds. Let’s double the number of objects created, to 1,600,000:
Student John is 23 years old and is studying Physics Student is old? 0 97.3272409408
That’s 97 seconds for 1,600,000 objects.
Now let’s do a comparative run using Java.
Running the Java Code
Listing 4 illustrates a simple Java program that also attempts to stress the platform a little.
Listing 4—The Java test program.
public class Student {String name;int age;String major;public Student() {// TODO Auto-generated constructor stub}public Student(String name, int age, String major) {this.name = name;this.age = age;this.major = major;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getMajor() {return major;}public void setMajor(String major) {this.major = major;}public static void main(String[] args) {long startTime = System.currentTimeMillis();for (int i = 0; i < 500000; i++) {Student student = new Student("John", 23, "Physics");System.out.println("Student " + student.getName() + " is "+ student.getAge() +" years old and is studying " + student.getMajor());}long estimatedTime = System.currentTimeMillis() - startTime;System.out.println("Time estimate: " + estimatedTime/1000);} }
Notice in Listing 4 that I’ve included automatically generated getter and setter methods. Experienced Eclipse Java developers use this feature all the time; the getters and setters are automatically generated by Eclipse. The same is true for the two constructors. This type of productivity enhancement is really handy, and because such code is machine-generated, it’s completely error-free.
Let’s run the Java code with 500,000 objects, just as we did for the Python case:
Student John is 23 years old and is studying Physics Student is old: false Time estimate: 31
That’s 31 seconds for 500,000 objects. Now we run it with 800,000 objects:
Student John is 23 years old and is studying Physics Student is old: false Time estimate: 50
That’s 50 seconds for 800,000 objects. Now we run our final Java test with 1,600,000 objects:
Student John is 23 years old and is studying Physics Student is old: false Time estimate: 104
That’s 104 seconds for 1,600,000 objects.
Let’s tabulate the results for comparison.
Comparative Speed Test
Number of Objects | Java Speed | Python Speed |
500,000 | 31 | 30 |
800,000 | 50 | 48 |
1,600,000 | 104 | 97 |
The test results show that the Python code outperforms the Java code by a small margin. This is not unexpected. Java might be called a “heavyweight” mainstream language; it comes with a certain amount of baggage, including but not limited to the following:
- Portability. This simply means that Java bytecode will run on any platform with an appropriate Java virtual machine (JVM).
- Type safety. As the example illustrates, type safety is closely related to memory safety. This language feature helps to avoid situations in which an attempt is made to copy an invalid bit pattern into a given memory area.
- Built-in security. The Java security model is based on a sandbox in which code can run safely with minimal negative effects on the underlying platform.
As with any technology, this feature set comes at a cost; however, as the table shows, the cost in the current test context is not exactly exorbitant.
This is our second takeaway: OO does have a cost, but it’s relatively cheap considering all the extra capabilities you get.
Extending the Test
The tests I ran for this example are pretty simple. A more realistic test might use objects that read and write to a database, or send and receive network traffic. If the data in such programs is derived from files, that would help in stressing the application with disk I/O.

Running the Code
Python can be run from the order line; all the more advantageously, you can run it from inside a coordinated advancement condition (IDE, for example, Eclipse. I want to utilize an IDE as a result of the numerous profitability improvements they offer: code age, unit testing, bundle, and module creation, etc.
Beginning with Python and Eclipse is simple: Install Eclipse and afterward utilize the Eclipse Marketplace to introduce the PyDev module. Make a Python (or PyDev) module, and you’re good to go to begin making your Python code.
Obviously, it’s significantly simpler to run the Java code in Eclipse, in light of the fact that the default establishment as of now incorporates uphold for Java. What’s more, we should not overlook all the subordinate Java profitability upgrades: code finishing, code age (getters, setters, constructors, and so forth.), refactoring, etc.
Notwithstanding your language decision or programming model (OO versus procedural or useful), there is no rejecting that the utilization of an advanced IDE, for example, Eclipse is a significant efficiency upgrade. This kind of hardware encourages nimble improvement as code age, refactoring, and apparatus reconciliation through modules.
Last Thoughts
OO dialects were the subject of a specific measure of question, harking back to the 1990s. Back then, numerous associations liked to stay with standard dialects, for example, C, instead of receiving the new C++. At that point along came Java, and I believe most would agree that C++ was no longer the de facto OO language.
These days, OO dialects are utilized in installed stages basically as per usual. Nonetheless, there is still some protection from utilizing the OO highlights in dialects, for example, Python and PHP. The explanations behind this obstruction may have more to do with developer inclinations than with the real world!
One intriguing part of correlation between OO code in various dialects is the shared characteristic between such dialects. Python OO code isn’t inconceivably not the same as the identical code in Java. This could be viewed as a preferred position of utilizing OO highlights in the multi-language time, helping software engineers to deliver great code. Less difficult code is commonly generally welcomed by upkeep developers and creation uphold staff.
The speed of such extensively proportionate Java and Python code is really comparative, as I’ve delineated here with basic tests, just as tantamount outcomes in my article “Information base Development: Comparing Python and Java ORM Performance.”
OO offers numerous favorable circumstances in any language. The expected simplicity of understanding that OO gives could be a solid inspiration for its utilization. Given this and different points of interest, OO appears to offer such a large number of pluses at the present time, and possibly later on, for brilliant developers to continue dodging it.
Source: http://www.informit.com/articles/article.aspx?p=2436668