473,804 Members | 3,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2775
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
3055
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 because they aren't useful from the outside anyway. Example: Before: class Mess(object): def complicated(self, count):
9
6223
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 __init__ method of the existing instance is called nonetheless, so that the instance is initialized a second time. For example, please consider the following class (a singleton in this case): >>> class C(object): .... instance = None .... def __new__(cls): .... if C.instance is None:
5
2505
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 what situations we must use __new__? Can __new__ take the place of __init__? Thanks.
5
1936
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 need to be able to inspect each class and get all of its instance data as well as information about a particular meathod. This is what i want it to do: class song:
13
1486
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 instance: class Foo: def __init__(self, val): """This is really slow.""" self.num = val
5
1243
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 an instance from another one without worrying about malicious code (ignoring, for now, malicious code involving attribute access) as shown below. >>> import types >>> class Foo: .... def __init__(self, who, knows, what, args): .... self.mystery_args = (who, knows, what, args)
1
1205
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 returned by A.__new__ is an instance of A. This can be observed by the following code: class A(object): def __new__(cls, *args, **kwds): print "A.__new__", args, kwds
5
1474
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 more specific errors. Preferably I want to do this by changing as little code as possible. I also want the new exceptions to be a subclass of the old S3ResponseError so as to not break old code that catches it. If I meet these two requirements I expect my modification to make it into boto and then...
5
1478
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 (__call__, etc.) of new-style class instances. In other words, I can do this: .... pass .... 33
0
9704
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
10562
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
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
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
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
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.