467,076 Members | 940 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,076 developers. It's quick & easy.

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
  • viewed: 2550
Share:
3 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Harry Pehkonen | last post: by
9 posts views Thread by Felix Wiemann | last post: by
5 posts views Thread by could ildg | last post: by
5 posts views Thread by Tim Henderson | last post: by
13 posts views Thread by Jeremy Sanders | last post: by
1 post views Thread by Frank Benkstein | last post: by
5 posts views Thread by Sandra-24 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.