The Surprise

His skin was leathery and torn. He could easily have been mistaken for an old man were it not for his childish features that still penetrated his battered exterior. He was looking down at a small…


独家优惠奖金 100% 高达 1 BTC + 180 免费旋转

Python Classes for Java programmers

The purpose of this post is to highlight some important differences between the two programming languages when dealing with classes and, there so, Object-Oriented programming. It is meant to be a introduction to the topic, which I will discuss it further on future posts.

As a fan of Game of Thrones and since its last season has just started, I’ve written a simple Game of Thrones example of the House’s members on both languages with the same output, in order to make it easier to compare. First lets take a look at the complete Java code, and after the Python version, containing code comments on each difference.

Let’s look at each class:

Example of a single inheritance, although Python supports multiple inheritance. Here we refer to parent class through super() function.

The title property is the python version of a getter. To declare one we make use of @property decorator. I won’t explain what decorators are now because It will be covered in future posts among other features I’m learning in Python. For now, just believe me that it is a getter :)

A private method also starts with double underscore. And finally, The way we declare a class method in Python is with @classmethod decorator.

Worth to mention that the new keyword is not needed and we usually can avoid the use of for loops with list and/or dictionary comprehension .

This was a quick introduction to classes in Python for those familiar with java. There is a lot more to cover, which I plan do to in the future.

Add a comment

Related posts:

6 Benefits of getting away for the weekend

While a full-out Vacation to a great destination is a wonderful thing, there are great benefits to be had from just a weekend or Long Weekend spent at a cottage, trailer, or other vacation home.

The Man in the Clock Tower

The clock tower stands forever in all its glory; inside, a library. In the attic is a man, gazing upon the world. He stands, silent, so observant. The clock keeps ticking and ticking, the man…