473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inherting from object. Or not.


(Picking up a side track of the "python without OO" thread.)

On Wednesday 26 January 2005 11:01, Neil Benn wrote:
I say this because you do need to be aware of the
'mythical python wand' which will turn you from a bad programmer into a
good programmer simply by typing 'class Klass(object):'.


What is the difference between inherting form object, and not doing it? E.g,
what's the difference between the two following classes?

class foo:
pass

class bar(object):
pass

Sometimes people inherit from it, and sometimes not. I don't see a pattern in
their choices.
Cheers,

Frans

Jul 18 '05 #1
7 1257
Hello Frans,
What you are seeing is a step on the path to unification of types and
classes.
Now we have that clear ;)
Classes that inherit from object are 'new style classes'.
They were introduced in 2.2 and they have different internal methods.
The ones that have no declaration is an 'old style class'.
http://www.python.org/doc/newstyle.html
That link may be help.
It is not required to write a class as 'class myclass(object)' yet, so
many don't. In some cases it will matter which you use. In others cases
it won't.
Be sure to try this an an interpreter promt:
Py> class NewKlass(object):
.... pass
Py> class OldKlass:
.... pass
Py> new = NewKlass()
Py> old = OldKlass()
Py> print dir(new)
Py> print dir(old)

Also new style classes are faster.
Hth,
M.E.Farmer

Frans Englich wrote:
(Picking up a side track of the "python without OO" thread.)

On Wednesday 26 January 2005 11:01, Neil Benn wrote:
I say this because you do need to be aware of the
'mythical python wand' which will turn you from a bad programmer into a good programmer simply by typing 'class Klass(object):'.
What is the difference between inherting form object, and not doing

it? E.g, what's the difference between the two following classes?

class foo:
pass

class bar(object):
pass

Sometimes people inherit from it, and sometimes not. I don't see a pattern in their choices.
Cheers,

Frans


Jul 18 '05 #2
> What is the difference between inherting form object, and not doing it? E.g,
what's the difference between the two following classes?

class foo:
pass

class bar(object):
pass

Sometimes people inherit from it, and sometimes not. I don't see a pattern in
their choices.

I think this is the best source:
http://www.python.org/2.2.3/descrintro.html

Subclassing object gets you a "new style" class and some
additional capabilities. Maybe eventually it will not
matter.
Jul 18 '05 #3
Frans Englich wrote:
What is the difference between inherting form object, and not doing it?


Although this doesn't provide a description of all the implications,
it does give you the basic answer to the question and you can easily
do further research to learn more:

http://www.python.org/doc/2.2.1/what...00000000000000

(Summary: inheriting from object gives you a "new-style" class,
not doing so gives you a classic class, and there are a
variety of differences resulting from that.)

-Peter
Jul 18 '05 #4
On Wednesday 26 January 2005 21:24, M.E.Farmer wrote:
Hello Frans,
What you are seeing is a step on the path to unification of types and
classes.


I changed all base classes in my project to inherit object. There appears to
be no reason to not do it, AFAICT.
Thanks,

Frans

Jul 18 '05 #5
Frans Englich wrote:
On Wednesday 26 January 2005 21:24, M.E.Farmer wrote:
Hello Frans,
What you are seeing is a step on the path to unification of types and
classes.

I changed all base classes in my project to inherit object. There appears to
be no reason to not do it, AFAICT.


Exactly. My advice is to use new-style classes unless you have a reason not to
(if you're inheriting from a builtin type, then there is no need to inherit from
object as well - the builtin types already have the correct basic type).

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #6
Nick Coghlan <nc******@iinet.net.au> wrote:
Exactly. My advice is to use new-style classes unless you have a
reason not to (if you're inheriting from a builtin type, then there
is no need to inherit from object as well - the builtin types
already have the correct basic type).


Except for Exception!

Exception and anything that inherits from it is an old style class.

I discovered the other day that you can't throw a new style class as
an exception at all, eg
class MyException(object): pass .... raise MyException Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: exceptions must be classes, instances, or strings (deprecated), not type
(not a terribly helpful message - took me a while to work it out!)

wheras old style works fine...
class MyOldException: pass .... raise MyOldException Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.MyOldException: <__main__.MyOldException instance at 0xb7df4cac>


After that I recalled a thread on python-dev about it

http://mail.python.org/pipermail/pyt...st/046812.html

--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Jul 18 '05 #7
Nick Craig-Wood wrote:
Nick Coghlan <nc******@iinet.net.au> wrote:
Exactly. My advice is to use new-style classes unless you have a
reason not to (if you're inheriting from a builtin type, then there
is no need to inherit from object as well - the builtin types
already have the correct basic type).

Except for Exception!

Exception and anything that inherits from it is an old style class.


And 'classobj' is the correct basic type for an Exception, since, as you
mentioned, new-style classes are currently unraisable :)

Cheers,
Nick.
I think there *is* work in progress to change that. . .

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
6
by: Madman | last post by:
CustDisplay Form (parent form) has the following declaration: Public Class CustDisplayForm Inherits System.Windows.Forms.Form 'Dim addCustsForm As New CustAddForm Dim editCustForm As New...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
0
by: Art | last post by:
This simple code works as expected in a regular asp.net page but doesn't when the same content is run from a page which inherited from a .master template. I literally copied the Button and the two...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.