I'M reading the book "Python Programing second edition for the absolute beginner". While explaining the object paramater whithin a class, this how it reads.

Base your class on object , a fundamental, built-in type.

Can someone please tell me what that means?
Thanks.

Difficult to say without more context. Is the author talking about inheritance at that point?

Note that in python 3, there are no 'old style class', so that your class is implicitely a subclass of object. Here is a small experiment with python 2.6 and python 3.1

Python 2.6.4 (r264:75706, Oct 27 2009, 15:50:27) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...  pass
... 
>>> class B(object):
...  pass
... 
>>> import inspect
>>> inspect.getmro(A) # get the ancestor classes
(<class __main__.A at 0x7f3356d339b0>,)
>>> inspect.getmro(B)
(<class '__main__.B'>, <type 'object'>)
>>> dir(A)
['__doc__', '__module__']
>>> dir(B)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> issubclass(A, object)
False
>>> a = A()
>>> isinstance(a, object) # look at this !
True
>>> issubclass(B, object)
True
Python 3.1.1 (r311:74480, Aug 17 2009, 21:52:39) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...  pass
... 
>>> class B(object):
...  pass
... 
>>> import inspect
>>> inspect.getmro(A)
(<class '__main__.A'>, <class 'object'>)
>>> inspect.getmro(B)
(<class '__main__.B'>, <class 'object'>)
>>> dir(A)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> dir(B)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> issubclass(A, object)
True
>>> a = A()
>>> isinstance(a, object)
True

The use of the new style class had a practical implication, it allowed the use of __slots__ as shown in this example ...

class Oldstyle:
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3
        print(self.__dict__)  # {'a': 1, 'c': 3, 'b': 2}


class Newstyle(object):
    """
    __slots__ prevents new variable creation outside the class
    and improves the speed by replacing the usual class __dict__
    """
    # 'register' the variables allowed
    __slots__ = ["a", "b", "c"]
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3
        #print(self.__dict__)  # will give error


oldobj = Oldstyle()
# allows creation of a new class variable outside the class
oldobj.d = 4 

newobj = Newstyle()
# attempt to create a new class variable outside the class
# gives an error, since d is not 'registered'
newobj.d = 4

Starting with Python3 all classes are new style whether you use (object) or not.

OK, I read the paper that was linked to above and the comments in this thread so far.

So back in the old days, before Python 2.3 we only had Classic Classes and we created them using this syntax

class Gato:
    def __init__(self):
        self.name="fluffy"

But now we're supposed to use the new style of classes which uses this syntax:

class Gato(object):
    def __init__(self):
        self.name="fluffy"

And then starting in Python 3.0 we will go back to declaring our classes the way we did back in the old days, but we will still get new style classes.

Am I right on this so far?

OK, I read the paper that was linked to above and the comments in this thread so far.

So back in the old days, before Python 2.3 we only had Classic Classes and we created them using this syntax

class Gato:
    def __init__(self):
        self.name="fluffy"

But now we're supposed to use the new style of classes which uses this syntax:

class Gato(object):
    def __init__(self):
        self.name="fluffy"

And then starting in Python 3.0 we will go back to declaring our classes the way we did back in the old days, but we will still get new style classes.

Am I right on this so far?

Correct. Don't you admire the beauty of Python version changes? Right out of a Monty Python show!

Actually, most computer languages have had their fair share of version changes, they are always for the better!

Oh yeah, I understand the version changes and I actually appreciate them. I'm just still trying to decide if I should start writing my classes with that object parameter or not. I'm not seeing what advantage it brings and it will be something I have to unlearn when I start using Python v3.

Python3 will tolerate adding (object) to your class for a while at least.
After that just write a little Python utility that removes it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.