Python Method After Create An Object
Problem: I need a special Method in Python that is executed AFTER the Object is created. Suppose i have a situation: ##-User Class; class User: __objList = []
Solution 1:
Just add your code to the __init__
function, when you create an object the code in the __init__
function will run.
classUser(object):
__objList = []
def__init__(self, name):
self.name = name
# append object to list after create object
self.__objList.append(self)
@classmethoddefshowObjList(cls):
for obj in cls.__objList:
print("obj: (%s), name: (%s)" % (obj, obj.name))
a = User("Joy")
b = User("Hank")
User.showObjList()
Post a Comment for "Python Method After Create An Object"