如何从python 2.6中的类内的对象访问方法? [英] How can I acess a metod from an object inside a class in python 2.6?

查看:41
本文介绍了如何从python 2.6中的类内的对象访问方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有一个包含很多功能和一对类的类(我认为是子类).主要问题是我希望子类调用在函数 init 中定义的对象,并且不知道如何引用它

In my code I have a class that contain a lot of functions and a pair of classes (subclass I think). The main problem is that I want the subclasses to call an object, defined in the function init and I don't know ho to referencing it

代码中的主要问题:

class principal:
    def __init__(self):
        self.a = object()
...

...
    class secondary(threading.Thread):
        def __init__(self):
            a.proceed() # problem !!!

我试图调用一个类似的principal.a.proceed(),但是Traceback告诉我:AttributeError:类主体没有属性"a"

I've tried to call a like principal.a.proceed() but the Traceback tells me: AttributeError: class principal has no attribute 'a'

我该如何解决.看来这很容易解决,但我看不到.

how can I solve this. It appear that it is something easy to solve, but I can't see it.

推荐答案

与Java不同,对于 principal 的每个实例,不会有一个新的 principal.secondary 类.当您在Python中的另一个类(一个类肽:P)中声明一个类时,这仅意味着将内部类作为外部类的类属性来访问,并且什么都不会.因此,没有 principal 的外部实例可以从 principal.secondary 方法访问.

Unlike Java, there will not be a new principal.secondary class for each instance of principal. When you declare a class inside another class (a classeption :P) in Python it only means that the inside class will be accessed as a class attribute of the outside class and nothing more. So, there is no outside instance of principal to be accessed from the principal.secondary methods.

实际上,您想在Python中做一些毫无意义的事情.我建议您通过其他方式来做您想做的事情.例如,您可以获取 principal 的实例,并将其作为 principal.secondary 的构造函数的参数传递:

Indeed, you want to do something meaningless in Python. I would suggest you do whatever you are trying to do by some other way. For example, you can get an instance of principal and pass it as parameter for the constructor of principal.secondary:

p = principal()
s = principal.secondary(p)

还要注意, principal.secondary 不是 principal 的子类,而只是一个类中的一个类.子类"是从另一个继承/继承的类.因此, principal.secondary threading.Thread 的子类,而不是来自主体.

Also, note that principal.secondary is not a subclass of principal but only a class inside a class. "Subclasses" are classes that extends/inherits from another one. So, principal.secondary is a subclass of threading.Thread, not from principal.

最后,有两个小建议:始终使您的类扩展 object (如果它不扩展任何其他类),并遵循

Finally, two little off-topic suggestions: always make your classes extend object if it does not extend any other class and follow the PEP-8 by starting the name of your classes by an uppercase letter:

class Principal(object):
    ...

编辑:如果您真的想将类绑定到类似于Java中的实例(我怀疑您想这样做),则有一种方法可以对其进行仿真.创建一个接收对象作为参数的函数,声明并返回类:

EDIT: If you really want to have a class bound to an instance like in Java (and I doubt you want),there is a way to emulate it. Create a function that receives an object as parameter, declare and return the class:

def generateSecondary(outerObject):
    class Secondary(object):
        def __init__(self):
            outerObject.proceed()
    return Secondary

outerObject 参数将绑定到函数内部声明的类.

The outerObject parameter will be bound to the class declared inside the function.

现在,声明您的 Principal 类.在 Principal __ init __ 方法中调用 generateSecondary()函数,并将初始化对象作为参数并将其设置为初始化对象的属性:

Now, declare your Principal class. Call the generateSecondary() function in the __init__ method of Principal passing the initializing object as parameter and setting it to an attribute of the initializing object:

class Principal(object):
    def __init__(self):
        self.attribute = "an attribute of %s" % self
        self.Secondary = generateSecondary(self)

    def proceed(self):
        print self.attribute

结果是:

>>> principal = Principal()
>>> secondary = principal.Secondary()
an attribute of <classeption.Principal object at 0x2e3970>
>>> secondary
<classeption.Secondary object at 0x2e3a10>

这是可能的,因为Python类是可以绑定到闭包的第一类对象(按传统意义上的闭包").我不建议使用这样的代码,但是有一种方法可以让您提出-这可能不是您需要的事情.

It is possible because Python classes are first-class objects that can be bound to closures (in the traditional sense of "closure"). I do not recommend to use such code but there is a way to do what you asked - which may not be what you need.

这篇关于如何从python 2.6中的类内的对象访问方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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