Home > Articles > Open Source > Python

Comparing Python Object-Oriented Code with Java

Do you avoid Python OO features, preferring the procedural/functional model? That tendency is common in languages such as PHP, where many programmers opt not to use OO features. But you might be missing an important opportunity! Java requires an OO approach, giving you the advantages of that model in every line of code you write. Stephen B. Morris points out the advantages of using Python's OO features in a manner similar to Java's.
Like this article? We recommend

Outline: Object Orientation

Programmers are creatures of habit, and we tend to stick with established language features unless we have some compelling reason to embrace new ones. Object-oriented (OO) features are a good example of this issue. PHP programmers consider PHP's OO features to be a good idea—but use them sparingly, if at all. The story is similar with many Python programmers, who prefer not to use Python's OO features.

Java sits at the other end of the language spectrum: It's an OO language, so there's no getting away from classes when you use Java. Despite Java's OO pedigree, however, a lot of Java code is still written in a procedural manner.

Why this bias against (or possible misuse of) OO? I think it boils down to a combination of personal inclination and engineering judgment. If a PHP or Python programmer has extensive experience with one of these languages and hasn't used the OO features often, the disinclination may be due to simple inertia. But certain development tasks might be better implemented in an OO context than in the familiar procedural/functional paradigm.

It's true that OO programming can result in issues such as heap fragmentation or other nondeterministic platform states, such as performance deterioration. Indeed, the issue of OO heap use was one reason why C++ took many years to replace C in embedded development projects. Back in the 1990s, disk, CPU, and memory were at such a premium that, at least in the minds of designers, they precluded the use of OO languages (which also precluded potential productivity gains from using these emerging languages).

I think it's still fair to say that many Python programmers avoid OO features unless no other option exists. In this article, I compare Python and Java to show how they stack up against each other in terms of complexity and speed. I hope this will allow for an objective assessment!

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 = name
        self.age = age
        self.major = major

    def 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.name to 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 time

class Student:
    def __init__(self, name, age, major):
        self.name = name
        self.age = age
        self.major = major

    def is_old(self):
        return self.age > 100

start = 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:

  1. Import the time module.
  2. Create a time snapshot at the beginning of the program.
  3. Instantiate a large number of Student objects.
  4. Access the data inside each object.
  5. Take a time snapshot and subtract the original time.
  6. 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 command line; more conveniently, you can run it from within an integrated development environment (IDE) such as Eclipse. I prefer to use an IDE because of the many productivity enhancements they offer: code generation, unit testing, package and module creation, and so on.

Getting started with Python and Eclipse is easy: Install Eclipse and then use the Eclipse Marketplace to install the PyDev plug-in. Create a Python (or PyDev) module, and you're all set to start creating your Python code.

Of course, it's even easier to run the Java code in Eclipse, because the default installation already includes support for Java. And let's not forget all the ancillary Java productivity enhancements: code completion, code generation (getters, setters, constructors, etc.), refactoring, and so on.

Regardless of your language choice or programming model (OO versus procedural or functional), there is no denying that the use of a modern IDE such as Eclipse is a major productivity enhancement. This type of tool facilitates agile development in the form of code generation, refactoring, and tool integration via plug-ins.

Final Thoughts

OO languages were the subject of a certain amount of mistrust back in the 1990s. In those days, many organizations preferred to stick with mainstream languages such as C, rather than adopting the new C++. Then along came Java, and I think it's fair to say that C++ was no longer the de facto OO language.

Nowadays, OO languages are used in embedded platforms pretty much as a matter of course. However, there is still some resistance to using the OO features in languages such as Python and PHP. The reasons for this resistance might have more to do with programmer preferences than with reality!

One interesting aspect of a comparison between OO code in different languages is the commonality between such languages. Python OO code is not vastly different from equivalent code in Java. This could be considered an advantage of using OO features in the multi-language era, helping programmers to produce good code. Simpler code is generally well received by maintenance programmers and production support staff.

The speed of such broadly equivalent Java and Python code is pretty similar, as I've illustrated here with simple tests, as well as comparable results in my article "Database Development: Comparing Python and Java ORM Performance."

OO offers many advantages in any language. The potential ease of understanding that OO provides could be a strong motivation for its use. Given this and the other advantages, OO seems to offer too many pluses right now, and potentially in the future, for smart programmers to keep avoiding it.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020