Python Class classname (Object)

jskfan
jskfan used Ask the Experts™
on
Python Class classname (Object)

I have the codes below if I declare the class Car(object) or just class Car, I get the same result. I am not sure what the keyword (Object) is for.

Thanks


class Car:

    wheels = 4

    def __init__(self, make, model):
        self.make = make
        self.model = model

mustang = Car('Ford', 'Mustang')
print (mustang.wheels)
# 4
print (Car.wheels)
# 4
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
These days, I suggest to use/learn Python 3. (And from using print with parentheses, it seems to be your case.) For Python 3, the base class object should not be used explicitly.

In Python 2, there were basically two approaches. (If I recall correctly, it was from quite old Python version on--like version 2.2 or so.) The later was called new-style classes, the older was called classic-style classes. The news style was always recommended since then, and it introduced the object base class. The object base class implemented some new functionality; so, the object base class had to be used explicitly whenever you wanted that functionality. To simplify, the rule was "always use object explicitly".

In Python 3, only the new-style classes are used (the classic-style classes are not implemented internally). This way, you can choose to use the object base class explicitly, or not. The result will be always the same. The object base class is always used.

To improve readability of your code, you should not use the object base class explicitly.

The only exception for using the object explicitly even in Python 3 code is when you write the code that should be used both by Python 2 and Python 3 (possibly when writing some libraries, but it will be eliminated in future as Python 3 will be prefered over Python 2 more and more).

Author

Commented:
Thank you for clarifying that up.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial