Difference between class classname: AND class classname(): AND class classname(object):
class, oop, python
Solution
Well, I can immediately say that there is nothing special about the second method:
class my_class():
content...
In the above code, the parenthesis are redundant and do not affect the class definition at all. The reason why Python allows you to have them is explained by @chepner's comment. Basically, because the list of base classes (the code placed inside the parenthesis) may be any valid expression, `()` is allowed because it is syntactically valid.
The purpose of the other two methods depends on what version of Python you are using.
Python 2.x
The following code will create an old-style or classic class:
class my_class:
content...
In Python versions before 2.2, old-style classes were the only type of classes available. According to Python's documentation on them:
The concept of (old-style) class is unrelated to the concept of type: if `x` is an instance of an old-style class, then `x.__class__` designates the class of `x`, but `type(x)` is always `<type 'instance'>`. This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance.
However, in Python versions 2.2 or greater, this all changes if you inherit from the `object` built-in:
class my_class(object):
content...
Now, you will be creating a new-style class, which has an entirely different object model. Unlike its predecessor, a new-style class:
...is neither more nor less than a user-defined type. If `x` is an instance of a new-style class, then `type(x)` is typically the same as `x.__class__`...
If you would like some more information on what the differences between old-style and new-style classes are, see this great SO question which asks exactly that.
Python 3.x
In Python 3.x, all classes implicitly inherit from `object` and are thus new-style classes.
This means that it is now redundant to do:
class my_class(object):
content...
because doing this:
class my_class:
content...
will yield the same results. However, many programmers still explicitly inherit from `object` to make their classes backwards-compatible with older versions of Python.
Problem
I'm learning python and introducing myself to OOP. However, I'm struggling to understand how best to build classes and particularly, what the difference between the following class definitions are and when I should use each: ``` class my_class: content... class my_class(): content... class my_class(object): content... ``` I have been reading the very useful python online help, although not found a specific answer to this question. So any ideas or recommended references would be appreciated, thanks.