Python Forum
function that run once for all objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function that run once for all objects
#11
You can simply override the __init__method after the first instance's creation
class Spam:
    def __init__(self):
        Spam.thingy = "eggs"
        _other_init(self)
        Spam.__init__ = _other_init
        print("Hey! I'm the first Spam instance'")

def _other_init(self):
    print("Initializing", self)

for i in range(3):
    Spam()
Output:
ens@thom:~$ python tmp/initruc.py Initializing <__main__.Spam object at 0x7fb34a8b14c0> Hey! I'm the first Spam instance' Initializing <__main__.Spam object at 0x7fb34a8b14c0> Initializing <__main__.Spam object at 0x7fb34a8b14c0>
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020