473,387 Members | 1,540 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.

class with invalid base class

Out of curiosity, I tried passing using an invalid base
for a class. I can't explain why I got the error messages
I did. Can someone here enlighten me?

# Here I'm just curious
def spam(a, b): .... return a+b
.... class Spam(spam): .... pass
....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: function() argument 1 must be code, not str
# What's 'function'? Why is it called?
class Spam(1): pass ....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int() takes at most 2 arguments (3 given)
# what were the three given arguments?
# is it something I can redefine?
class Report: .... def __getattr__(self, name):
.... print "Trying to get", repr(name)
.... raise AttributeError(name)
.... class Spam(Report()): pass ....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: this constructor takes no arguments
# doesn't look like it. What if I derive from an instance
# derived from object?
class Report(object): .... def __getattr__(self, name):
.... print "Trying to get", repr(name)
.... raise AttributeError(name)
.... class Spam(Report()): pass ....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: default __new__ takes no parameters
# Okay.... Don't know what's going on, so I'll
# just fiddle around a bit.
class ABCD: .... def __init__(self): pass
.... class Spam(ABCD()): pass ....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: __init__() takes exactly 1 argument (4 given) class ABCD: .... def __init__(self, a): pass
.... class Spam(ABCD()): pass ....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: __init__() takes exactly 2 arguments (1 given)
# Which is it; 4 given or 1 given? And
# int had 3 passed to it....
class XYZZY: .... def __init__(self, **args): print "I have", args
.... class Spam(XYZZY()): pass ....
I have {}
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: __init__() takes exactly 1 argument (4 given)


Comments?

Andrew
da***@dalkescientific.com
Jul 18 '05 #1
2 2817
"Andrew Dalke" <ad****@mindspring.com> writes:
Out of curiosity, I tried passing using an invalid base
for a class. I can't explain why I got the error messages
I did. Can someone here enlighten me?

# Here I'm just curious
def spam(a, b): ... return a+b
... class Spam(spam): ... pass
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: function() argument 1 must be code, not str
# What's 'function'? Why is it called?


It's the Don Beaudry hook ;-). If the base class has callable a
__class__ attribute, this is called with three arguments: The name of
the new class, a tuple of the bases, and a dictionary. Or something like
this, the details may be wrong...

Since now functions have a __class__ attribute, the hook is triggered by
your code above:
def spam(a, b): .... return a + b
.... print spam.__class__ <type 'function'> spam.__class__("Spam", (spam,), {}) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function() argument 1 must be code, not str


Thomas
Jul 18 '05 #2
Thomas Heller <th*****@python.net> writes:
"Andrew Dalke" <ad****@mindspring.com> writes:
Out of curiosity, I tried passing using an invalid base
for a class. I can't explain why I got the error messages
I did. Can someone here enlighten me?

# Here I'm just curious
> def spam(a, b):

... return a+b
...
> class Spam(spam):

... pass
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: function() argument 1 must be code, not str
>


# What's 'function'? Why is it called?


It's the Don Beaudry hook ;-). If the base class has callable a
__class__ attribute, this is called with three arguments: The name of
the new class, a tuple of the bases, and a dictionary. Or something like
this, the details may be wrong...


Actually, I think it's type(baseclass) that gets called -- otherwise
you wouldn't be able to inherit from old-style classes! What's
changed since 2.2 is that type objects are now callable.

(Isn't it amazing how something as simple as metaclasses -- and they
*are* a simple idea -- can be *so* confusing? I wrote and rewrote
that paragraph at least three times...)

Cheers,
mwh

--
But since your post didn't lay out your assumptions, your goals,
or how you view language characteristics as fitting in with
either, you're not a *natural* candidate for embracing Design by
Contract <0.6 wink>. -- Tim Peters, giving Eiffel adoption advice
Jul 18 '05 #3

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

Similar topics

42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
0
by: Mark Parter | last post by:
I'm trying convert an XML Schema (http://www.elframework.org/projects/xcri/efc_r1.0.xsd/view) to a VB.Net class using the XSD tool provided with the .NET 2 SDK. However, it's failing to generate...
6
by: noel.hunt | last post by:
I have a base class, PadRcv, with virtual functions. User code will derive from this class and possibly supply it's own functions to override the base class virtual functions. How can I test that...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.