472,334 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

new style classes, __new__, __init__

Hi,

i have some questions related to new style classes, they look
quite useful but i wonder if somebody can give me an example
on constructors using __new__ and on using __init__ ?

I just see that when using it i can't give parameters to __new__
and when i additionally define __init__ then __new__ is not
called.

So i can use __new__ only for classes whose constructors don't
have parameters?
class C2:
def __new__(self):
print "new called"
self.a = 8

def __init__(self, a):
print "init called"
self.a = a

def fct(self):
print self.a
a = C2(7)
a.fct()
This way __new__ is not called, if i remove __init__ then
there are too many parameters to __new__, if i add a parameter
to __new__ then it says that __new__ does not take arguments.
Thanks for any hints,
Torsten.

Sep 16 '08 #1
3 2692
Torsten Mohr wrote:
Hi,

i have some questions related to new style classes, they look
quite useful but i wonder if somebody can give me an example
on constructors using __new__ and on using __init__ ?

I just see that when using it i can't give parameters to __new__
and when i additionally define __init__ then __new__ is not
called.

So i can use __new__ only for classes whose constructors don't
have parameters?
class C2:
def __new__(self):
print "new called"
self.a = 8

def __init__(self, a):
print "init called"
self.a = a

def fct(self):
print self.a
a = C2(7)
a.fct()
This way __new__ is not called, if i remove __init__ then
there are too many parameters to __new__, if i add a parameter
to __new__ then it says that __new__ does not take arguments.
Thanks for any hints,
Torsten.
New style classes should be based on object:

class C2(object):

-Larry
Sep 16 '08 #2
Hello,
This way __new__ is not called, if i remove __init__ then
there are too many parameters to __new__, if i add a parameter
to __new__ then it says that __new__ does not take arguments.
I just found an article that describes it better, this example works:

class C2(object):
def __new__(cls, a):
obj = object.__new__(cls)
print "new called"
obj.a = 8

return obj

__new__ = staticmethod(__new__)
def __init__(self, a):
print "init called"
self.a = a

def fct(self):
print self.a
a = C2(7)
a.fct()
Best regards,
Torsten.

Sep 16 '08 #3
Torsten Mohr wrote:
I just found an article that describes it better, this example works:

class C2(object):
def __new__(cls, a):
obj = object.__new__(cls)
print "new called"
obj.a = 8

return obj

__new__ = staticmethod(__new__)
Staticmethod isnt' required here.
Christian

Sep 16 '08 #4

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

Similar topics

7
by: Harry Pehkonen | last post by:
I have been defining new class methods when I'm trying to simplify some code. But I'm thinking I should just define functions within that method...
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...
5
by: could ildg | last post by:
As there is already __init__, why need a __new__? What can __new__ give us while __init__ can't? In what situations we should use __new__? And in...
5
by: Tim Henderson | last post by:
Hello I want to creat a program that can inspect a set of classes that i have made and spit out a savable version of these classes. To do this I...
13
by: Jeremy Sanders | last post by:
Is it possible to implement some sort of "lazy" creation of objects only when the object is used, but behaving in the same way as the object? For...
5
by: Devan L | last post by:
Is there any safe way to create an instance of an untrusted class without consulting the class in any way? With old-style classes, I can recreate...
1
by: Frank Benkstein | last post by:
Hi, the behaviour I always observed when creating instances by calling the class A is that '__init__' is always only called when the object...
5
by: Sandra-24 | last post by:
Ok here's the problem, I'm modifying a 3rd party library (boto) to have more specific exceptions. I want to change S3ResponseError into about 30...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.