如何将实例强制转换为派生类? [英] How do you cast an instance to a derived class?

查看:135
本文介绍了如何将实例强制转换为派生类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我正在使用的Python程序中使用一点继承。我有一个基类User,它实现了用户的所有功能。我添加了一个未经批准的用户的概念,就像用户一样,增加了一个方法。

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 concept of an unapproved user, which is just like a user, with the addition of a single method.

User类有一些返回用户的方法目的。当我进行子类化时,这将不起作用,因为我最终会让一个UnapprovedUser返回一个User,阻止我调用这个方法,等等。

The User class has some methods that return a User object. This will not work when I subclass, since I will end up having an UnapprovedUser return a User, preventing me from calling this method, among other things.

class User(object):
    base_dn = 'ou=Users,dc=example,dc=org'

    @classmethod
    def get(cls, uid):
        ldap_data = LdapUtil.get(uid + ',' + self.base_dn)
        return User._from_ldap(ldap_data)

class UnapprovedUser(User):
    base_dn = 'ou=UnapprovedUsers,dc=example,dc=org'

    def approve(self):
        new_dn = '' # the new DN
        LdapUtil.move(self.dn, new_dn)

get() _from_ldap()两个类的方法相同,但UnapprovedUser中的 get()方法需要返回一个UnapprovedUser对象,而不是一个User。

The get() and _from_ldap() methods are the same for both classes, though the get() method in UnapprovedUser needs to return an UnapprovedUser object, not a User.

如何从 User.get()<转换我获得的一个User实例? / code>进入Unapprov edUser?

How can I cast one of the instances of User that I get from User.get() into an UnapprovedUser?

我想做类似的事情:

class UnapprovedUser(User):
    # continued from before

    @classmethod
    def get(cls, uid):
        user = super(UnapprovedUser, cls).get(uid)
        return (UnapprovedUser) user # invalid syntax

以便我可以从父级包装方法并简单地将返回的值转换为正确的类。然后,这样做会导致父母使用他们的 self.base_dn 的值,这将破坏一切。

so that I can wrap the method from the parent and simply cast the returned value to the correct class. Then again, doing it that way could lead to the parent using their value for self.base_dn, which would break everything.

推荐答案

我觉得你真的想要创建一个 UnapprovedUser 而不是而不是强制转换。用户调用 UnapprovedUser.get()时。要做到这一点:

Rather than "casting", I think you really want to create an UnapprovedUser rather than a User when invoking UnapprovedUser.get(). To do that:

更改 User.get 实际使用 cls 传入的参数:

Change User.get to actually use the cls argument that's passed-in:

@classmethod
def get(cls, uid):
    ldap_data = LdapUtil.get(uid + ',' + self.base_dn)
    return cls._from_ldap(ldap_data)

你需要在 _from_ldap 中做类似的事情。您没有列出 _from_ldap 的代码,但我认为在某些时候它会执行以下操作:

You'll need to do something similar in _from_ldap. You didn't list the code for _from_ldap, but I assume that at some point it does something like:

result = User(... blah ...)

您想要将其替换为:

result = cls(... blah ...)

请记住:在Python中,类对象是一个可调用的,用于构造该类的实例。因此,您可以使用classmethod的 cls 参数来构造用于调用classmethod的类的实例。

Remember: in Python a class object is a callable that constructs instances of that class. So you can use the cls parameter of a classmethod to construct instances of the class used to call the classmethod.

这篇关于如何将实例强制转换为派生类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆