473,405 Members | 2,261 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,405 software developers and data experts.

subclassing a module: misleading(?) error message

I ran into a problem I didn't understand at first. I got part of it figured
out. Let me first demonstrate the original problem:
cat Super.py
class Super(object):
def __init__(self):
self._class = 'Super'
def hello(self):
print "%s says 'Hello'" % self._class
cat Sub.py
import Super

class Sub(Super):
def __init__(self):
self._class = 'Sub'
>
python
Python 2.3.4 (#1, Feb 7 2005, 15:50:45)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>from Super import Super
from Sub import Sub
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "Sub.py", line 4, in ?
class Sub(Super):
TypeError: function takes at most 2 arguments (3 given)
>>>
My question is NOT "What's wrong here?"
(The answer to that is that the import in Sub.py should be: from Super
import Super
i.e., I tried to use the module itself where I meant to subclass the class
defined in that module).

My questions are:

Why does python complain about a function here? (it's a class definition
statement, right?)
Is there really a function being called here?
If so:
What function was called?
What two arguments is it expecting?
What three were given?

Thanks,
-ej
Jan 4 '07 #1
4 11012
Erik Johnson wrote:
My questions are:

Why does python complain about a function here? (it's a class definition
statement, right?)
Because you're calling the function with the wrong number of arguments.
Is there really a function being called here?
Yes. (Well, it's not exactly a function, but you are calling
something.)
If so:
What function was called?
types.ModuleType
What two arguments is it expecting?
The name of the module and a docstring.
What three were given?
1. The name of the class
2. The tuple of bases
3. A dict containing the symbols defined inside the class statement.

You are aware, I presume, that types (be it classes or built-in types)
are callable. For example, to create an instance of class of class X,
you would call X(). And to create a list, you can call list(). Well,
in the same way, you can call type(), with the three arguments above,
to create classes. And that's just what the class statement does under
the covers (usually).

The following class definition:

class A(object):
b = 1

is exactly equivalent to this explicit call to type:

A = type("A",(object,),{"b":1})

However, there are some cases where, instead of creating the class
object itself, type will instead call some other function to create it.
One way is if you define __metaclass__ in the class namespace: then
type will call the object spec. Another way is if there are any bases
which have a type other than type. (Remember, bases are supposed to be
type objects.)

That's what happened to you. type saw that type(Super) was
types.ModuleType, not type, so it called that instead of creating the
class itself. However, types.ModuleType doesn't accept that same
arguments that a normal type constructor does, so you get the error.

Does that clear things up? Probably not. For a detailed explanation
of how this all works, look for some resources on learning
"metaclasses" or "metatypes" in Python.
Carl Banks

Jan 4 '07 #2
So you know you are subclassing a module.

There is an answer @
http://www.velocityreviews.com/forum...38&postcount=2

On Jan 4, 3:49 pm, "Erik Johnson" <ej at somewhere.comwrote:
I ran into a problem I didn't understand at first. I got part of it figured
out. Let me first demonstrate the original problem:
cat Super.pyclass Super(object):
def __init__(self):
self._class = 'Super'
def hello(self):
print "%s says 'Hello'" % self._class
cat Sub.pyimport Super

class Sub(Super):
def __init__(self):
self._class = 'Sub'
pythonPython 2.3.4 (#1, Feb 7 2005, 15:50:45)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>from Super import Super
>from Sub import SubTraceback (most recent call last):
File "<stdin>", line 1, in ?
File "Sub.py", line 4, in ?
class Sub(Super):
TypeError: function takes at most 2 arguments (3 given)

My question is NOT "What's wrong here?"
(The answer to that is that the import in Sub.py should be: from Super
import Super
i.e., I tried to use the module itself where I meant to subclass the class
defined in that module).

My questions are:

Why does python complain about a function here? (it's a class definition
statement, right?)
Is there really a function being called here?
If so:
What function was called?
What two arguments is it expecting?
What three were given?

Thanks,
-ej
Jan 4 '07 #3
At Thursday 4/1/2007 17:49, Erik Johnson wrote:
>Python 2.3.4 (#1, Feb 7 2005, 15:50:45)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "Sub.py", line 4, in ?
class Sub(Super):
TypeError: function takes at most 2 arguments (3 given)

Why does python complain about a function here? (it's a class definition
statement, right?)
Is there really a function being called here?
If so:
What function was called?
What two arguments is it expecting?
What three were given?
The same thing on Python 2.4.2 (at least) prints a much more meaningful error:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 5 '07 #4
ej

Carl Banks wrote:

<snipA good explanation

I have not been able to get back to news lately - lot going on, but
thank you for your time to explain that to me. It mostly makes pretty
good sense to me, but I will have to study metaclasses further. ;)

Thanks,
-ej

Jan 11 '07 #5

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

Similar topics

1
by: Beowulf | last post by:
Hello, I'm using Python to automate admin tasks on my job. We use Windoze 2000 as desktop platform. When executing this daily backup scripts I get the following error: Traceback (most recent...
0
by: Calvin Spealman | last post by:
I've been working on a small test runner script, to accumulate my test scripts (all python files in the 'test' sub-directories of my source tree). Things were going well, but I'm still having...
1
by: Mullin Yu | last post by:
my web services is reference a vb6 com dll, and if it's registered at the local machine, the web service is working. but, if i put the dll to a z: which is mapping to a network driver and...
0
by: timewatchdeveloper | last post by:
Hi all I posted this under IE and am reposting here in the hope that you can help! I maintain a system that subclasses IE browser when run. This has been working perfectly but recently one of the...
8
by: Claudio Grondi | last post by:
Here an example of what I mean (Python 2.4.2, IDLE 1.1.2, Windows XP SP2, NTFS file system, 80 GByte large file): Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel-...
5
by: saif.shakeel | last post by:
#!/usr/bin/env python from elementtree import ElementTree as Element tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): if t.get("Semantics") == "localId":...
8
by: Peter Pei | last post by:
Try the following to see it <?php class A { } function foo(A $a = 1) { ; } foo(); ?>
5
by: sawilla | last post by:
First, I'm new to Python. I'm getting and error when I run Python 2.5.2 as a regular user in Vista but not when I run Python as an administrator. For example, if I type "import numpy" after I...
4
by: vincehofmeister | last post by:
Hey everyone: I am using py2exe and everything is working fine except one module, ClientCookie, found here: http://wwwsearch.sourceforge.net/ClientCookie/ Keeps coming up as not found no...
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
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
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...
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,...
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
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...
0
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...

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.