How do you cast an instance to a derived class?

前端 未结 5 1899

I am trying to use a little inheritance in a Python program I am working on. I have a base class, User, which implements all of the functionality of a user. I am adding the co

5条回答
  •  旧巷少年郎
    2021-02-13 23:31

    In a class method, the class is passed in in the cls parameter. So instead of User.something do cls.something. Done!

    That said, I'm not sure I would do this with two types of user. I'm not 100% sure what you mean with "Approved" here, I it seems to me to be one of two things.

    1. It may mean the user isn't really logged in yet. In that case I'd have a special Anonymous User instance for not logged in users. Since you are moving the DN when approving, this seems more likely to be what you are doing.

    2. It may mean that the user hasn't been approved as a full member or something. This is just a special case of permission handling, and you are probably going to end up wanting to have more permissions later. I'd instead add support for giving the user roles, and making "Approved" a role.

    If you mean something else with approved, feel free to ignore this. :-)

提交回复
热议问题