473,771 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What are new-style classes?

I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!

Aug 28 '05 #1
12 1591
Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


There's a link on the left sidebar of http://docs.python.org

http://www.python.org/doc/newstyle.html

--
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

Aug 28 '05 #2
Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!

Older Pythons have a dichotomy between programmer-declared object
classes (from which subclasses can inherit) and the built-in object
classes (which just existed as built-in, but which could not be used as
the base of subclasses).

More recently the object hierarchy was redesigned, and now everything
inherits from object, so (if you know what you are doing) you can
implement subclasses of the built-in types such as dict, float and str.

Robert Kern has given you a link that explains the situation in detail.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Aug 28 '05 #3
On Sunday 28 August 2005 04:47 am, Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes. The simplest is "object", so the simplest newstyle class is:

class no_class(object ):
pass

as opposed to the simplest "old style" object which didn't inherit at all:

class really_no_class :
pass

I would regard the latter as deprecated now, since it basically doesn't
buy you anything to use it. The only reason to hang on to old style
classes would seem to be to avoid breaking older code that relied on
details such as the order of multiple inheritence, which have changed.

So if you're just learning, just use new style classes exclusively, and
use the documentation that applies to them. I think it's fairly non-
controversial that new style classes are an improved design.

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Aug 28 '05 #4
Terry Hancock wrote:
On Sunday 28 August 2005 04:47 am, Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes.


[Warning, advanced stuff ahead!]

That's not entirely true. New-style classes need not be derived from a new-
style class, they need to use the metaclass "type" or a derived.

So you can also declare a new-style class as

class new_class:
__metaclass__ = type

Or, if you want to switch a whole module with many classes to new-style, just set a

__metaclass__ = type

globally.
Reinhold
Aug 28 '05 #5
Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


In short: They have inherited "object" from somewhere. (This is
probably technically inaccurate, but it's the only thing I have seen
which defines a 'new-style class').

What it means *practically*, is that you can use properties. For a
long while I didn't really understand the point or properties, until I
needed them.

I have written a flowcharting application in Python. All objects on
the flowchat are derived from a base object which has the attribute
"text" (all objects have some form of, sometimes internal only, title).
Problem is that when some objects change text, I want to perform some
recalculations. So I wrote a "text" property, which - in its
settext-method - does all the calculations if required.

That way, the application can till use:

obj.text = 'Blah'

...and still have the chance to act on the text change.

I could have simply written a setText() method, but imho properties
are neater - but that's just a matter of opinion.
--
Kind Regards,
Jan Danielsson
Te audire no possum. Musa sapientum fixa est in aure.
Aug 29 '05 #6
Reinhold Birkenfeld wrote:
Terry Hancock wrote:
On Sunday 28 August 2005 04:47 am, Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes.

[Warning, advanced stuff ahead!]

That's not entirely true. New-style classes need not be derived from a new-
style class, they need to use the metaclass "type" or a derived.

So you can also declare a new-style class as

class new_class:
__metaclass__ = type

Or, if you want to switch a whole module with many classes to new-style, just set a

__metaclass__ = type

globally.
Reinhold

What are the pros and cons of the alternate approach?

Colin W.
Aug 30 '05 #7
Colin J. Williams wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!

"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes.

[Warning, advanced stuff ahead!]

That's not entirely true. New-style classes need not be derived from a new-
style class, they need to use the metaclass "type" or a derived.

So you can also declare a new-style class as

class new_class:
__metaclass__ = type

Or, if you want to switch a whole module with many classes to new-style, just set a

__metaclass__ = type

globally.

What are the pros and cons of the alternate approach?


The customary way is to use "class new_class(objec t):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.

Reinhold
Aug 30 '05 #8
On Tuesday 30 August 2005 04:09 pm, Reinhold Birkenfeld wrote:
The customary way is to use "class new_class(objec t):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.


Nonsense. "__metaclas s__" is simply an implementation detail.

We know that because it begins with "__".

Therefore it is invisible, and any delusion you may have that
you can see it is a complete non-issue.

In Python we call that encapsulation.

;-D

Cheers,
Terry

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Aug 31 '05 #9
Terry Hancock wrote:
On Tuesday 30 August 2005 04:09 pm, Reinhold Birkenfeld wrote:
The customary way is to use "class new_class(objec t):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.
Nonsense.


Given the rest of your post, I assume that this isn't meant as it sounds. Remember, I'm
German, so please bear with my sense of humour. ;)
"__metaclas s__" is simply an implementation detail.

We know that because it begins with "__".

Therefore it is invisible, and any delusion you may have that
you can see it is a complete non-issue.

In Python we call that encapsulation.

;-D


Reinhold
Aug 31 '05 #10

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

Similar topics

92
6541
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption? cheers, reed
125
14850
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
15
2460
by: M.Siler | last post by:
<HTML> <HEAD> <TITLE></TITLE> <SCRIPT> <!-- var factor_val = new Array(8,7) factor_val = 68.8 factor_val = 55
10
2058
by: Steven T. Hatton | last post by:
I read Stroustrup's article of the day: http://www.research.att.com/~bs/C++.html Programming with Exceptions. InformIt.com. April 2001. http://www.research.att.com/~bs/eh_brief.pdf Some of these ideas are finally beginning to sink in. I believe I looked at the same article a while back and decided I wasn't quite ready for it. If I understood things correctly, there seems to be a slight problem with the design of his exception safe...
2
3092
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing this by aggregating new content from all databases into a single indexed database and then saving a timestamp in the account database (for the current user) that tells me when the user last read items in the aggregated database.
26
4491
by: Lasse Edsvik | last post by:
Hello I'm trying to build a simple COM+ app in vs.net using C# and i cant register it in component manager..... what more is needed than this: using System; using System.EnterpriseServices;
9
2823
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
21
5225
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each table and 11 sub items with data about each column. When a Row in ListView1 is selected the Data in ListVies2 is loaded to show the correct data. Initially the first row in ListView1 is selected in FormX load
4
1178
by: Hyphessobrycon | last post by:
Private Sub btnrubriekbij_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrubriekbij.Click 'insert hier Dim cn As New OleDb.OleDbConnection(constr) Dim dc As New OleDb.OleDbCommand dc.Connection = cn
98
4625
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
0
9619
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
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9910
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
8933
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
7460
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
6712
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.