Is there any way to call functions inside a class without triggering the __init__
part of that class? Let's say I have the next class, usually I'd call the function this way:
有没有办法在不触发该类的__init__部分的情况下调用类中的函数?假设我有下一个类,通常我会用这种方式调用函数:
class Potato():
def __init__(self):
print("Initializing")
def myfunction(self):
print("I do something")
Potato().myfunction()
But as expected that prints the Initializing part. Now, If I wanted to call myfunction without triggering that. How would you do it? Pros and cons of doing it? It's even possible?
但正如预期的那样,打印出Initializing部分。现在,如果我想调用myfunction而不触发它。你会怎么做?这样做的优点和缺点?它甚至可能吗?
3 个解决方案
#1
3
Yes, it is possible.
对的,这是可能的。
Using a helper function
You could write a helper function that replace __init__
method of your class with a dummy method and then instantiates the class and after this we re-assign the old __init__
back to class.
您可以编写一个辅助函数,用一个伪方法替换您的类的__init__方法然后实例化该类,然后我们将旧的__init__重新分配回类。
def skip_init(cls):
actual_init = cls.__init__
cls.__init__ = lambda *args, **kwargs: None
instance = cls()
cls.__init__ = actual_init
return instance
Demo:
演示:
>>> a = skip_init(Potato)
>>> a.myfunction()
I do something
Overriding __new__
You could override __new__
method of your class and there based on argument you can replace __init__
with a dummy method.
你可以覆盖你的类的__new__方法,并根据参数你可以用一个虚方法替换__init__。
def new_init(cls, init):
def reset_init(*args, **kwargs):
cls.__init__ = init
return reset_init
class Potato():
def __new__(cls, *args, **kwargs):
instance = object.__new__(cls)
lazy = kwargs.pop('_no_init', False)
if not lazy:
return instance
cls.__init__ = new_init(cls, cls.__init__)
return instance
def __init__(self):
print("Initializing")
def myfunction(self):
print("I do something")
Demo:
演示:
>>> a = Potato(_no_init=True)
>>> a.myfunction()
I do something
>>> b = Potato()
Initializing
>>> b.myfunction()
I do something
#2
4
Not sure if this is what you're looking for, but:
不确定这是否是您正在寻找的,但是:
A classmethod or a staticmethod could be used without creating an instance of the class - which means that init would not be called that way:
可以使用classmethod或static方法而无需创建类的实例 - 这意味着不会以这种方式调用init:
class MyClass:
def __init__(self):
print("Initialize instance - not used")
@staticmethod
def my_static(toprint):
print(toprint)
MyClass.my_static("Print this")
#3
0
1) You can create a class without an init but it's going to behave more like a function.
1)你可以创建一个没有init的类,但它的行为更像是一个函数。
2) If you use the __init__
method, then it's going to be called everytime the class is called, but you can avoid it triggering variables if you so chose. Note the method will still get called.
2)如果使用__init__方法,那么每次调用类时都会调用它,但是如果你这样选择的话,你可以避免它触发变量。请注意,该方法仍将被调用。
class Potato():
def __init__(self,init_or_dont=1):
self.init_or_dont = init_or_dont
if self.init_or_dont:
print("Initializing")
def myfunction(self):
print("I do something")
Potato(init_or_dont=0).myfunction()
#1
3
Yes, it is possible.
对的,这是可能的。
Using a helper function
You could write a helper function that replace __init__
method of your class with a dummy method and then instantiates the class and after this we re-assign the old __init__
back to class.
您可以编写一个辅助函数,用一个伪方法替换您的类的__init__方法然后实例化该类,然后我们将旧的__init__重新分配回类。
def skip_init(cls):
actual_init = cls.__init__
cls.__init__ = lambda *args, **kwargs: None
instance = cls()
cls.__init__ = actual_init
return instance
Demo:
演示:
>>> a = skip_init(Potato)
>>> a.myfunction()
I do something
Overriding __new__
You could override __new__
method of your class and there based on argument you can replace __init__
with a dummy method.
你可以覆盖你的类的__new__方法,并根据参数你可以用一个虚方法替换__init__。
def new_init(cls, init):
def reset_init(*args, **kwargs):
cls.__init__ = init
return reset_init
class Potato():
def __new__(cls, *args, **kwargs):
instance = object.__new__(cls)
lazy = kwargs.pop('_no_init', False)
if not lazy:
return instance
cls.__init__ = new_init(cls, cls.__init__)
return instance
def __init__(self):
print("Initializing")
def myfunction(self):
print("I do something")
Demo:
演示:
>>> a = Potato(_no_init=True)
>>> a.myfunction()
I do something
>>> b = Potato()
Initializing
>>> b.myfunction()
I do something
#2
4
Not sure if this is what you're looking for, but:
不确定这是否是您正在寻找的,但是:
A classmethod or a staticmethod could be used without creating an instance of the class - which means that init would not be called that way:
可以使用classmethod或static方法而无需创建类的实例 - 这意味着不会以这种方式调用init:
class MyClass:
def __init__(self):
print("Initialize instance - not used")
@staticmethod
def my_static(toprint):
print(toprint)
MyClass.my_static("Print this")
#3
0
1) You can create a class without an init but it's going to behave more like a function.
1)你可以创建一个没有init的类,但它的行为更像是一个函数。
2) If you use the __init__
method, then it's going to be called everytime the class is called, but you can avoid it triggering variables if you so chose. Note the method will still get called.
2)如果使用__init__方法,那么每次调用类时都会调用它,但是如果你这样选择的话,你可以避免它触发变量。请注意,该方法仍将被调用。
class Potato():
def __init__(self,init_or_dont=1):
self.init_or_dont = init_or_dont
if self.init_or_dont:
print("Initializing")
def myfunction(self):
print("I do something")
Potato(init_or_dont=0).myfunction()