小编Tom*_*zyk的帖子

Python - 为什么它不创建对象的新实例?

我有一点我不明白的问题.

我有一个方法:

def appendMethod(self, newInstance = someObject()):
    self.someList.append(newInstace)
Run Code Online (Sandbox Code Playgroud)

我称之为没有属性的方法:

object.appendMethod()
Run Code Online (Sandbox Code Playgroud)

实际上,我使用someObject的相同实例附加列表.

但是,如果我将其更改为:

def appendMethod(self):
    newInstace = someObject()
    self.someList.append(newInstance)
Run Code Online (Sandbox Code Playgroud)

我每次都得到该对象的新实例,有什么区别?

这是一个例子:

class someClass():
    myVal = 0

class otherClass1():

    someList = []

    def appendList(self):
        new = someClass()
        self.someList.append(new)

class otherClass2():

    someList = []

    def appendList(self, new = someClass()):
        self.someList.append(new)

newObject = otherClass1()
newObject.appendList()
newObject.appendList()
print newObject.someList[0] is newObject.someList[1]
>>>False

anotherObject = otherClass2()
anotherObject.appendList()
anotherObject.appendList()
print anotherObject.someList[0] is anotherObject.someList[1]
>>>True
Run Code Online (Sandbox Code Playgroud)

python class object instance

6
推荐指数
1
解决办法
2412
查看次数

matplotlib - 多边形边缘的半径 - 是否可能?

我在matplotlib中绘制一个多边形.我输入了所有坐标点.在某些点之间我想要'圆形'或'径向'边缘而不是直线(比如图中的点1和2).这可能吗?如果不是,最有效的绘制方法是什么?

示例图

编辑:Rutger的解决方案运作良好.

在此输入图像描述

python polygon matplotlib

6
推荐指数
1
解决办法
978
查看次数

标签 统计

python ×2

class ×1

instance ×1

matplotlib ×1

object ×1

polygon ×1