473,796 Members | 2,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting from object

Hello,

To create a classic (old style) class, I write :

class foo:
pass

To do the equivalent as a new style class, I write :

class foo(object):
pass

*Should* I in fact write :

class foo(object):
def __init__(self, *args, **kwargs):
object.__init__ (self)

?

Also, can anyone explain any tangible benefit of inheriting from
object, when not explicitly using any features of new style classes ?

Thanks :-)

Fuzzyman
http://www.voidspace.org.uk/python

Jul 19 '05 #1
16 2089
Fuzzyman wrote:
Hello,

To create a classic (old style) class, I write :

class foo:
pass

To do the equivalent as a new style class, I write :

class foo(object):
pass

*Should* I in fact write :

class foo(object):
def __init__(self, *args, **kwargs):
object.__init__ (self)

?
I don't believe so.
Also, can anyone explain any tangible benefit of inheriting from
object, when not explicitly using any features of new style classes ?


This class might not, but you (or someone else) may decide later to
subclass it and want to use those features. It's a good habit to get
into even if you don't initially plan on using those features.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #2
Fuzzyman a écrit :
Hello,

To create a classic (old style) class, I write :

class foo:
pass

To do the equivalent as a new style class, I write :

class foo(object):
pass

*Should* I in fact write :

class foo(object):
def __init__(self, *args, **kwargs):
object.__init__ (self)

?
Nope.
Also, can anyone explain any tangible benefit of inheriting from
object, when not explicitly using any features of new style classes ?


old-style classes are deprecated. They are still in the language (for
how much time ?) for compatibility reasons, but they won't last forever.
That should be a good enough reason to avoid them, given that the
only thing you have to do is to inherit from object...
Jul 19 '05 #3
Fuzzyman wrote:
Also, can anyone explain any tangible benefit of inheriting from
object, when not explicitly using any features of new style classes ?


One reason is that properties won't work correctly.
--
Benji York
Jul 19 '05 #4
So theres no actual advantage that you know of ;-)

Surely when they are removed :

class foo:
pass

won't become invalid syntax, it will just automatically inherit from
object ?

That's what I assumed, anyway....

Regards,

Fuzz
http://www.voidspace.org.uk/python

Jul 19 '05 #5
Fuzzyman wrote:
Surely when they are removed :

class foo:
pass

won't become invalid syntax, it will just automatically inherit from
object ?


Well, Guido views this particular syntax as an "oopsie"[1]. It should
actually look like:

class C():
pass

Guido also suggests that the explicit:

class C(object):
pass

is "much preferred"[2] over:

__metaclass__ = type

class C:
pass

when creating new-style classes. It's not exactly clear where Guido
comes down on your particular issue, but if I had to guess, I would
guess that he prefers the explicit reference to "object".

STeVe

[1]http://mail.python.org/pipermail/python-dev/2005-February/051706.html
[2]http://mail.python.org/pipermail/python-dev/2005-February/051711.html
Jul 19 '05 #6
Steven Bethard wrote:
Guido also suggests that the explicit:

class C(object):
pass

is "much preferred"[2] over:

__metaclass__ = type

class C:
pass


Really? I have been toying with the idea of using the __metaclass__
trick, since it results in completely valid Jython 2.1 code, as long as
you don't use any of the new-style features.

But once Brian Zimmer gets his broadband fixed, we're supposed to get a
new alpha release. =)

Dave
Jul 19 '05 #7
The reason I ask is that I often (well... a couple of times anyway) see
cryptic omments like :

and if you inherit from object you get all the benefits of new
style classes

Now I know about the advantages of inheriting from the built in types
(if that's what you want to do) -but am a bit fuzzier on the 'general
benefits'.

I'm vaguely aware of properties.... I'll have to explore them at some
point.

Best Regards,

Fuzzy
http://www.voidspace.org.uk/python

Jul 19 '05 #8
Bruno Desthuilliers <bd************ *****@free.quel quepart.fr> wrote:
Fuzzyman a écrit :
*Should* I in fact write :

class foo(object):
def __init__(self, *args, **kwargs):
object.__init__ (self)

?

Nope.


And if you were to do so, surely:

class foo(object):
def __init__(self, *args, **kwargs):
super(foo, self).__init__( self)

would be the preferred way to go?

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jul 19 '05 #9
Sion Arrowsmith wrote:
... And if you were to do so, surely:
class foo(object):
def __init__(self, *args, **kwargs):
super(foo, self).__init__( self)

would be the preferred way to go?

Or, perhaps:
class foo(object):
def __init__(self, *args, **kwargs):
super(foo, self).__init__( self, *args, **kwargs)
...

--Scott David Daniels
Sc***********@A cm.Org
Jul 19 '05 #10

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

Similar topics

11
1801
by: Christopher J. Bottaro | last post by:
I actually want all the parent classes too. So if D derives off C derives off B derives off A, I ultimately want a tuple ('D', 'C', 'B', 'A'). For those of you following the Python Documentation thread, this is a good example of how the PHP manual is "better". I found how to do this in a few seconds in PHP. I searched the Python docs for "class name", "classname", "introspection" and "getclass". I looked in the Class section of the...
15
1715
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter);
0
1308
by: Joe Sullivan | last post by:
I am working on some VB dot net classes and am inheriting from a class that has a method that returns a general object. My problem is, of course, is when i want to return an integer using this method. Ok, so I create a class to encapsulate the interger as an Object. Now, my question is how can I now treat that object as an integer? I do not want the user of my method to have to do this sort of thing: dim intIntPlusOne as Integer =...
11
2169
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
2
4046
by: Shayne H | last post by:
I wanted to create a type-safe collection by inheriting from CollectionBase. Public Class MyCollection : Inherits CollectionBase I found that IList.Add did not allow the setting of a "key" to identify an object added to the collection. So to implement a "key" similar to the parameter allowed by the standard Collection object do I have to create my own array to store the "keys" and manage it within my class MyCollection? I can't see how...
3
5387
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE bar ( attribute integer NOT NULL ) INHERITS (foo);
1
1280
by: Fabiano Sidler | last post by:
Hi folks! As stated in subject, how do I decide wether to inherit <type 'type'> or <type 'object'>? Whenever I want to intantiate my derived type, I taked <type 'type'> here, but inheriting from <type 'object'> consequently would be reasonable in cases of pure static objects (i.e. objects/types using staticmethods exclusively), for whose I would prefer toplevel code outside a class definition in python, since python does not oblige...
17
2450
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
1
2672
by: a | last post by:
Hi I would like to add some additional custom features to the listview, like cell editing. The created object will be added to the form as part of the GUI, and it should be able to handle events. I will create a class which conains the listview. Should I inherit this class from any existing class? If yes, which class should I inherit from? Or, should I just inherit my class from listview and add my add my additional features?
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.