同一个Python对象中的实例变量如何同时引用不同的对象?



我有两个参数列表[{Parameter(arcpy.Parameter)}][{arcpy.Parameter}]。与基类arcpy.Parameter相比,子类Parameter包含了额外的属性和功能。

下面是调试控制台的输出。param在列表1中,param2在列表2中。过滤器对象在基类中由类变量filter引用。对param.filter的更改也应该对param2.filter生效,反之亦然,因为它们应该引用同一个实例。

id(param)
86840096L
id(param2)
86840096L
id(param.filter)
62961424L
id(param2.filter)
62961368L

为什么过滤器id可以同时不同,虽然参数id是相同的?这意味着什么?我能阻止(或忽略)这一点吗?

class Parameter(mixins.ParameterMixin,_BaseArcObject):
filter = passthrough_attr('filter')
...
class Filter(_BaseArcObject):
"""The filter object allows you to specify the choices available for a
parameter."""
type = passthrough_attr('type')
list = passthrough_attr('list')
def passthrough_attr(prop):
"Basic attribute passthrough for a wrapped Arc object -- allows for early binding."
def get_(self):
"Geoprocessor %s property" % prop
return getattr(self._arc_object, prop)
def set_(self, val):
return setattr(self._arc_object, prop, val)
return property(get_, set_)
class _BaseArcObject(object):
_arc_object = None
def __init__(self, *args, **kwargs):
"""Wrapper for ArcGIS scripting Arc Objects --
Create a new object instance if no reference is passed in."""
super(_BaseArcObject, self).__init__()
self._arc_object = gp._gp.CreateObject(self.__class__.__name__,
*((arg._arc_object if hasattr(arg, '_arc_object') else arg)
for arg in args))
...

大胆猜测,如果filter是一个方法,它们是在每次访问函数时动态创建的。

>>> class A:
...    def foo(self):
...        pass

>>> type(A.foo)
function
>>> id(A.foo)
2528402192848
>>> id(A.foo)
2528402192848
>>> a = A()
>>> type(a.foo)
method
>>> id(a.foo)
2528397338368
>>> id(a.foo)
2528397337664

注意最后两个id不同。

相关内容

最新更新