class Knights vs class Knights(object)

View: New views
2 Messages — Rating Filter:   Alert me  

class Knights vs class Knights(object)

by Wayne Werner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, 

For class definitions I've always used

class Knights:

but I've just seen an example using:

class Knights(object):

So I did a quick little test and see this:

>>> a = Knights()
>>> b = Knights2()
>>> a
<__main__.Knights instance at 0xb7e12bec>
>>> b
<__main__.Knights2 object at 0xb7e12b2c>

and my question is what is the difference between the two? Is there a difference other than one is an object the other is an instance? I googled "python object vs. instance" and didn't find anything terribly useful.

Thanks,
Wayne

--
To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: class Knights vs class Knights(object)

by Patrick Sabin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Wayne Werner wrote:

> and my question is what is the difference between the two? Is there a
> difference other than one is an object the other is an instance? I
> googled "python object vs. instance" and didn't find anything terribly
> useful.

Yes there is a difference. One class inherits from object, the other
doesn't. You may want to try to check this, e.g.:

issubclass(Knight, object)

So the first keyword you may want to google for is inheritance.

The second keyword is old/new-style classes.

Python 2 changed the way how classes work, but to be backward compatible
the old mechanism still remained. If you want new style classes inherit
from object.

If you want to understand the details you may want to look up what
metaclasses - your third keyword - are, old-style classes have the
metaclass classobj, new-style classes type. You can check this using the
builtin type-function.

Beware that this is python 2 stuff. In python 3 class X: and class
X(object): are the same.

- Patrick
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor