0

I want to know if is there any way to make an object of a class, but don't call its __init__ method :

Look at the below code :

>>> class test():
    def __init__(self):
        print("an object created of this class")


>>> a=test()
an object created of this class
>>> 

I want a way to create an object of class test(), but don't print an object created of this class.

Is there any way?

Update: Assume that I implemented this class already and Instantiated 20 objects of it and I was need to its __init__ method for all of them. And now I want to instantiate some new objects, but I don't want to call its __init__ method any more. Is there any way?

ti4programing
  • 13
  • 1
  • 4
  • Do you mean in general create a class without an `__init__`, or avoid calling the already-implemented `__init__` of an existing class? What is it in `__init__` that you don't want to happen when the instance is created (and why is that in `__init__` at all if that's the case)? – jonrsharpe Oct 03 '14 at 12:30
  • I'm assuming this is a simplified example, and that your actual application was a lot more code hidden in __init__, my suggestion would be to simple add an extra keyword argument to init and if it's set to true then it returns rather then execute the rest of __init__ – James Kent Oct 03 '14 at 12:31
  • @jonrsharpe Yes, this `__init__` method is a part of an already-implemented class and I need its `print()` statement in the other parts of my program, I just want it to didn't call for one specific object! (Just curiosity) – ti4programing Oct 03 '14 at 12:35
  • 1
    Perhaps there should be a `verbose` argument to define whether these `print`s happen, or use `logging`, or ... It's hard to advise with such an abstract example. – jonrsharpe Oct 03 '14 at 12:38
  • @jonrsharpe please take a look at update section. – ti4programing Oct 03 '14 at 12:45
  • 1
    The update doesn't really make things clearer - more specific numbers doesn't really clarify the issue (also, there's no need to include a separate "Update" section, interested users can see the change history anyway). Generally, if there is code in `__init__` that doesn't need to run for the initialisation of all instances of the class, **move it**. – jonrsharpe Oct 03 '14 at 12:47

2 Answers2

4

@classmethod can be used to implement alternative ways to instantiate an object. All instances will call __init__, but additional code can be run in the classmethod:

class Test():
    def __init__(self):
        pass

    @classmethod
    def make_test(cls):
        t = cls()
        print("an object created of this class")
        return t

tests = [Test.make_test()  for i in range(3)]
# an object created of this class
# an object created of this class
# an object created of this class

newtest = Test()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 3
    +1 This nicely separates code that actually initializes an object from code that simply executes at the time of initialization. – chepner Oct 03 '14 at 12:58
  • Thank you, This is the best way to implement my class and I'll use it.But I think @chepner 's answer is the answer of my question, so I keep it checked. – ti4programing Oct 03 '14 at 13:11
  • What @chepner said. Invoking side-effects in __init__ doesn't seem like good design to me, so I can't figure out why someone would _want_ to print in an `__init__` call, apart from a temporary print for debugging purposes during development. – PM 2Ring Oct 03 '14 at 13:18
3

You can call __new__ directly, but I think you should probably think again about the design of your __init__ method instead.

>>> class A(object):
...   def __init__(self):
...     print "In __init__"
...
>>> a = A()
In __init__
>>> b = A.__new__(A)
>>> type(b)
<class '__main__.A'>
chepner
  • 497,756
  • 71
  • 530
  • 681