简体   繁体   English

调用在类外的类中定义的python方法

[英]Call python method defined out of class within the class

import math

class Circle(object):    
    def __init__(this,x,y,rad):
        this.x=x
        this.y=y
        this.rad=rad

    def relate(circ1,circ2):
        diff=__posDiff((circ1.x,circ1.y),(circ2.x,circ2.y))
        print diff

def __posDiff(p1,p2):
    diff=math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
    return diff

when I try to run the above code I get the following error: 当我尝试运行上面的代码时,出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Circle.py", line 18, in relate
    diff=__posDiff((circ1.x,circ1.y),(circ2.x,circ2.y))
NameError: global name '_Circle__posDiff' is not defined

Quite new to python, cant figure out how to call the function inside class.If anyone can help and explain python非常陌生,无法弄清楚如何在类内部调用该函数。

Rename __posDiff to _posDiff (so remove one of the leading underscores). __posDiff重命名为_posDiff (因此删除前导下划线之一)。

You are using double underscores at the start of the name, and when using such a name within a class definition, Python mangles that name to produce a class-private name. 您在名称的开头使用双下划线 ,并且在类定义中使用这样的名称时,Python会对该名称进行修饰以生成类专用名称。 This feature is used to add a namespace to methods that should not accidentally be overridden in subclasses. 此功能用于将名称空间添加到不应在子类中意外覆盖的方法。

This applies both to method names defined within the class, and any code that tries to use such a name . 这既适用于类中定义的方法名称,也适用于任何尝试使用该名称的代码 As such, your reference to __posDiff within the relate method is rewritten to _Circle__posDiff (the class name is prepended to create a namespace), but the __posDiff function itself is itself not re-named because it is not actually inside the class. 因此,您的参考__posDiff的内relate方法改写为_Circle__posDiff (类名都附加在创建命名空间),但__posDiff函数本身本身不是重新命名,因为它是不实际的类中。

See the Reserved classes of identifiers section in the lexical analysis documentation: 请参阅词法分析文档中的“标识符保留类”部分

__*
Class-private names. 类私有名称。 Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes. 当在类定义的上下文中使用时,此类别中的名称将被重写以使用变形的形式,以帮助避免基类和派生类的“私有”属性之间的名称冲突。 See section Identifiers (Names) . 请参阅“ 标识符(名称)”部分

And the linked Identifiers (Names) section in the expressions reference: 表达式参考中的链接标识符(名称)部分

Private name mangling : When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. 私有名称修饰 :当在类定义中以文本形式出现的标识符以两个或多个下划线字符开头且不以两个或多个下划线结尾时,则被视为该类的私有名称 Private names are transformed to a longer form before code is generated for them. 在为专用名称生成代码之前,专用名称会转换为更长的格式。 The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. 转换将在类名前面插入类名,并删除前导下划线,并插入单个下划线。 For example, the identifier __spam occurring in a class named Ham will be transformed to _Ham__spam . 例如,出现在名为Ham的类中的标识符__spam将转换为_Ham__spam This transformation is independent of the syntactical context in which the identifier is used. 此转换独立于使用标识符的句法上下文。

Bold italic emphasis is mine. 斜体字是我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM