【问题标题】:How to add method to a class and where to use "self" in python如何向类添加方法以及在 python 中在何处使用“self”
【发布时间】:2015-05-05 14:15:05
【问题描述】:

我是 python 语言的新手,我正在尝试理解 self 的概念。

我已经定义了以下两种方法,我想知道必须在代码中进行哪些更改才能将它们包含在一个类中以及在哪里使用self

import maya.cmds as cmds
# This function returns the animCurves connected to particular node 

def animCurveNode():

    animCurveName = []
    mySel = cmds.ls(sl = True)
    filePath = "D:\sdk.txt"
    attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
    for each in mySel:
        list = cmds.listConnections(each, d = False,s = True)
        animCurveName.extend(list)

    return animCurveName

# This function return the animCurves and the name of the atribute to connect as the first memebr in the list

def attrAnimCurve():
    attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
    animCurveNames = animCurveNode()
    animCurveNames.insert(0,attrName)
    return animCurveNames

【问题讨论】:

  • 为什么要在这里使用一个类?了解self 的用途非常重要。

标签: python class methods initialization self


【解决方案1】:

你所拥有的不是方法。他们不在任何班级。请看下面最简单的例子:

import maya.cmds as cmds


class MyClass:

    # This method returns the animCurves connected to particular node
    def animCurveNode(self):

        animCurveName = []
        mySel = cmds.ls(sl = True)
        filePath = "D:\sdk.txt"
        attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
        for each in mySel:
            list = cmds.listConnections(each, d = False,s = True)
            animCurveName.extend(list)

        return animCurveName

    # This method return the animCurves and the name of the atribute to connect as the first memebr in the list
    def attrAnimCurve(self):
        attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
        animCurveNames = animCurveNode()
        animCurveNames.insert(0,attrName)
        return animCurveNames

但是,您并没有真正指定要使用 self 做什么。例如,哪些变量应该是对象的一部分。

有关self 的更多信息,请查看以下内容:

What is the purpose of self?

【讨论】:

  • 如果它们之间不共享任何数据,那么将这些函数作为类的一部分是没有意义的。
  • 这是我在回答中试图表达的。 OP 应该首先说明他为什么要使用一个类。
【解决方案2】:

self 通常是类方法(绑定到类实例的函数)的第一个参数。 self 用作有问题的reference to the object instance,因此用于读取和写入该实例的属性。

对象引用的这种显式传递还允许在类定义之外设置方法:

class Foo:
  def __init__(self, data):
    self.data = data
  def get(self): return self.data

foo = Foo([1,2,3])
Foo.get_data_as_set = lambda self: set(self.data)
print foo.get()
print Foo.get_data_as_set(foo)

以上印刷品

[1, 2, 3]
set([1, 2, 3])

【讨论】:

    【解决方案3】:

    感谢您的回复... 我对哪些变量应该是对象的一部分的概念感到困惑。 例如在我的代码中

    class MyClass:
    
        # This method returns the animCurves connected to particular node
        def animCurveNode(self):
    
            animCurveName = []
            mySel = cmds.ls(sl = True)
    

    或者应该是

    class MyClass:
    
        # This method returns the animCurves connected to particular node
        def animCurveNode(self):
    
            self.animCurveName = []
            self.mySel = cmds.ls(sl = True)  
    

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 2021-09-08
      • 2018-08-14
      • 1970-01-01
      • 2017-10-30
      • 2021-10-21
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      相关资源
      最近更新 更多