473,396 Members | 1,933 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,396 software developers and data experts.

__init__(self, *args, **kwargs) - why not always?

Normally, I expect a subclass to act in a manner consistent
with its Base classes. In particular, I don't expect to
*lose* any functionality, unless that was the whole point of
the subclass. (e.g., a security-restricted version, or an
interface implementation that doesn't require a filesystem.)

One (common?) exception seems to occur in initialization. I
understand stripping out arguments that your subclass explicitly
handles or replaces. A subclass intended to restrict access
might even "handle" all unknown arguments. But for the general
case, is there any reason *not* to pass unhandled initializer
arguments to the parent classes?

More specifically, is there any reason not to replace:

class SubClass(BaseClass):
def __init__(self):
BaseClass.__init__(self)

with:

class SubClass(BaseClass):
def __init__(self, *args, **kwargs):
BaseClass.__init__(self, *args, **kwargs)

on a near-global basis?

--------

Bug 876421 provides an example surprise in the standard library.

http://sourceforge.net/tracker/?func...&group_id=5470

SubClass(level=ERROR) throws an exception, even though
BaseClass(level=ERROR) works, and level=ERROR was still
valid to the subclass, with the same meaning.

I expect that variations on verbosity will be a common
pass-through argument in many types of code.

--------

Yes, I realize that changing BaseClass.__init__(...)
to super.__init__(...) may be better still, but that
discussion is orthogonal to this question.

Yes, I would have preferred that the interpreter add
the (possibly modified) *args and **kwargs automatically,
but I do realize that it is too late for that change.

--
-jJ Take only memories. Leave not even footprints.
Jul 18 '05 #1
2 9609
Jim Jewett wrote:
More specifically, is there any reason not to replace:

class SubClass(BaseClass):
def __init__(self):
BaseClass.__init__(self)

with:

class SubClass(BaseClass):
def __init__(self, *args, **kwargs):
BaseClass.__init__(self, *args, **kwargs)

on a near-global basis?


A subclass may be a specialized case, e.g.:

class Tree:
def __init__(self, evergreen=False):
...

class Spruce(Tree):
def __init__(self):
Tree.__init__(self, True)

or the other way around, e.g.

class Enemy:
def __init__(self, pos):
...

class ShootingEnemy(Enemy):
def __init__(self, pos, bullets):
Enemy.__init__(pos)
...

In both cases I don't want to BaseClass.__init(self, *args, **kwargs)...

yours,
Gerrit.

--
105. If the agent is careless, and does not take a receipt for the
money which he gave the merchant, he can not consider the unreceipted
money as his own.
-- 1780 BC, Hammurabi, Code of Law
--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #2
I'm still not sure I understand the objection, as I would
still have wanted the extension even in your examples.

So I've gone into a bit more detail, and generalized the
replacement.
Is there any reason not to replace replace:

class SubClass(BaseClass):
def __init__(self, [what you used to take]):
BaseClass.__init__(self, [what you used to send])

with:

class SubClass(BaseClass):
def __init__(self, [what you used to take], *args, **kwargs):
BaseClass.__init__(self, [what you used to send], *args, **kwargs)

on a near-global basis?

Gerrit Holl <ge****@nl.linux.org> wrote in message
news:<ma**************************************@pyt hon.org>...
A subclass may be a specialized case, e.g.: class Tree:
def __init__(self, evergreen=False):
... class Spruce(Tree):
def __init__(self):
Tree.__init__(self, True)
I do understand that you will sometimes need to
modify the argument (or keywords) based on your
subclass' needs. My question is about unexpected
input -- which you might get if the base class is
later enhanced. What is wrong with the following:

class Spruce(Tree):
"Spruce are evergreen."
def __init__(self, *args, **kwargs)
eg = kwargs.pop('evergreen', True)
Tree.__init__(self, eg, *args, **kwargs)

or

class Spruce(Tree):
"Spruce are always evergreen, you annoying user!"
def __init__(self, *args, **kwargs)
kwargs['evergreen'] = True
Tree.__init__(self, *args, **kwargs)
These will work even if tree is later enhanced to keep
track of which trees need extra attention.

class Tree:
def __init__(self, evergreen=False, healthy=True):
...
or the other way around, e.g. class Enemy:
def __init__(self, pos):
... class ShootingEnemy(Enemy):
def __init__(self, pos, bullets):
Enemy.__init__(pos)
... In both cases I don't want to BaseClass.__init(self, *args, **kwargs)...


class ShootingEnemy(Enemy):
def __init__(self, pos, bullets, *args, **kwargs):
Enemy.__init__(self, pos, *args, **kwargs)

indicates that the base class also needed to see position.
This wouldn't take any special thought that wasn't already
required by passing position in your original example.

And again, it would remove some fragility in case the Enemy
class is later extended to indicate which army it is from,
or what type of unit.

--

-jJ Take only memories. Leave not even footprints.
Jul 18 '05 #3

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

Similar topics

3
by: Alexander Eberts | last post by:
I'm new to python so appologies to the group if this question is asked often. I'm wondering if it's possible to query an object instance to find out what arguments the instance's __init__ function...
3
by: paul kölle | last post by:
Hi list, in the course of writing a small app, I tried to design a class, which would allow to derive its behaviour solely from its name, so that I would be able to write one abstract class with...
9
by: Felix Wiemann | last post by:
Sometimes (but not always) the __new__ method of one of my classes returns an *existing* instance of the class. However, when it does that, the __init__ method of the existing instance is called...
1
by: yang | last post by:
I just a newbie of python Now I found that almost every program use Tkinter have this line class xxx(xxx): """xxxxx""" def __init__(self): """xxxxx""" Frame.__init__(self)
3
by: James Stroud | last post by:
Hello All, I did this: py> class bob(object): .... def __init__(self,**kwargs): .... for fname,func in kwargs.items(): .... setattr(self, fname, lambda *args : func(*args)) .......
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
2
by: flogic | last post by:
Hi i m a newbie to python .. jus started to learn ...am quite confused about variable arguments used in python functions and in init. i dont where to use **keys , **kwds,*args ...etc... if...
4
by: Steven D'Aprano | last post by:
When you call a new-style class, the __new__ method is called with the user-supplied arguments, followed by the __init__ method with the same arguments. I would like to modify the arguments...
25
by: Erik Lind | last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more online and can write simple modules, but I still don't get when __init__ needs to be used as opposed to creating a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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...
0
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,...

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.