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

type() takes one or *three* arguments!?

I was looking at Simon Burton's Povray.py code (part of pypov) and saw
this line:
globals()[name] = type( name, (KWItem,), {} ) # nifty :)

where 'KWItem' was a class. It did seem nifty, but it was unclear to me
what was happening.

I went to python.org's online documentation which said that type()
takes one argument. So I fired up python:
type(42) <type 'int'> type("x", (type(42),), {})

<class '__main__.x'>

OK, It appears that type() with 3 arguments constructs a class. Is this
documented somewhere? If not can someone explain what is going on?
james

Jul 18 '05 #1
2 1626
Hi,

Up to Python 2.2, type() was just a function to return an object type.
From 2.2 on, type have this behavior when called with only one argument and is used to create a new type when called with 3 arguments.

From http://www.python.org/2.2/descrintro.html :
"The signature of type() requires an explanation: traditionally, type(x)
returns the type of object x, and this usage is still supported.
However, type(name, bases, methods) is a new usage that creates a brand
new type object. (This gets into metaclass programming, and I won't go
into this further here except to note that this signature is the same as
that used by the Don Beaudry hook of metaclass fame.)"

So, an example:

Foo = type('Foo', (object,), {})

and...

class Foo(object):
pass

Are the same thing...


On 30 Jan 2005 11:57:23 -0800
ja****************@gmail.com wrote:
I was looking at Simon Burton's Povray.py code (part of pypov) and saw
this line:
globals()[name] = type( name, (KWItem,), {} ) # nifty :)

where 'KWItem' was a class. It did seem nifty, but it was unclear to
me what was happening.

I went to python.org's online documentation which said that type()
takes one argument. So I fired up python:
type(42) <type 'int'> type("x", (type(42),), {})

<class '__main__.x'>

OK, It appears that type() with 3 arguments constructs a class. Is
this documented somewhere? If not can someone explain what is going
on? james

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #2
Thank you - that explains everything quite nicely.

Jul 18 '05 #3

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

Similar topics

4
by: T. Kaufmann | last post by:
Hi there, A simple but important question: How can I initialize a super class (like dict) correctly in my subclass constructor? A sample: class MyClass(dict): def __init__(self):
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
4
by: Harold Howe | last post by:
I am running into a situation where the compiler complains that it cannot infer the type parameters of a generic method when one of the function arguments is an anonymous method. Here is a...
4
by: hyderabadblues | last post by:
I have problem.I want to create function pointer with typedef so that I can use it for both functions which takes one integer parameter and function which take none because of some obvious reasons...
0
by: David McClosky | last post by:
Hi everyone, I'm stumped over what is probably (hopefully?) a very simple problem. Suppose I have a class that multiply inherits from two classes, call them A and B. A inherits from a class...
13
by: Gabriel Genellina | last post by:
En Mon, 06 Oct 2008 11:19:58 -0300, Joe Strout <joe@strout.netescribió: Apart from the wise words that others have said, I'd add that I see no point in identifier prefixes when they merely...
0
by: Martin Vilcans | last post by:
Thanks everyone for the suggestions. I've implemented a simple solution using sys.settrace. It's quite nice because it doesn't require any instrumentation of the code (it works like a debugger that...
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
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
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.