954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why the parent of a class is - object - ?

Hello everyone,

I have been through the Starting Python thread, and I have archived lots of stuff for further learning !

But there is one little thing for which there is no specific explanation, and looking at Google doesn't help me much.

Why do the classes of many scripts are not written like that:

class NameAClass: def ...

But instead like this:class NameAClass(object):
def ... Here is a post containing one script like that

What does it brings to the code ? What does is the reference behind the object "object" ?
What is the use for that and what kind of problems does it solve ?

Thanks !

Kezoor
Newbie Poster
15 posts since Oct 2008
Reputation Points: 11
Solved Threads: 0
 

This is so because of the history of the python language. In the early versions of python, classes were defined using class A: . Then the implementation of python objects changed, mainly so that users could define subclasses of the builtin types like int, str, dict, list, etc.The 'new style' classes appeared and were defined using class A(object) . The first kind of definition was kept so that old python code could still run. You should always use new style classes in your code (some properties of the old style objects differ from the others)

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Like when you want to create class that will create simple dialog and then call it from GUI you have created:

import wx
    class Dialogi(wx.Dialog):
        ...........
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

Just wanted to add that in python 3.0 every class wil be an newstyle class so you better use those to make sure you can easy porte your code to that version of the language

mathijs
Light Poster
41 posts since Oct 2007
Reputation Points: 10
Solved Threads: 2
 

Thanks all ! That's the kind of little thing, that you can't keep you from thinking about it when your program doesn't work and don't know why ...

Kezoor
Newbie Poster
15 posts since Oct 2008
Reputation Points: 11
Solved Threads: 0
 
Just wanted to add that in python 3.0 every class wil be an newstyle class so you better use those to make sure you can easy porte your code to that version of the language

And that's information that will be useful very quickly, as python 3.0 is coming out !

Because most of the 'traditional' tutorials doesn't write the code like that...

Kezoor
Newbie Poster
15 posts since Oct 2008
Reputation Points: 11
Solved Threads: 0
 

Me again !

I have found a VERY interesting paper on objects and classes, this way please !

http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html

It is a definitive explication on why "classes" inherit of "object" ! Concise, well illustrated and to the point !

A must-see IMHO

Kezoor
Newbie Poster
15 posts since Oct 2008
Reputation Points: 11
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You