In some cases, I want to use instance method in class creation time.
I just want to use self in permissions list..This is my question
But it does not work. Is it some ways to solve the problem?
class PermissionChecker(object):
# how to use self in class create time.
permissions = [self.is_superuser(), self.is_god()]
def is_superuser(self):
# use self.property just like self.name...
return True
def is_god(self):
return True
class Child(PermissionChecker):
permissions = PermissionChecker.permissions + [self.is_coder(),]
def is_coder(self):
return True
It looks like you are confusing the term
attributewithproperty; you cannot useselfin the class definition itself, it is only available to a method.I think you are looking for the
propertydescriptor:By using
property(above used as a@decorator) you turn a method into an attribute:The
permissionsmethod is called every time the.permissionsattribute is accessed on an instance.Alternatively, set the list in the
__init__initializer: