Python 访问初始化变量

Python 访问初始化变量,python,python-3.x,Python,Python 3.x,我想知道是否有一种方法可以从类外访问coords变量。假设,我无法将坐标更改为self.coords class S_shape(Shape): def __init__(self, center): coords = [Point(center.x, center.y), Point(center.x, center.y + 1), Point(center.x + 1, cente

我想知道是否有一种方法可以从类外访问coords变量。假设,我无法将坐标更改为self.coords

class S_shape(Shape):
    def __init__(self, center):
        coords = [Point(center.x,     center.y),
                  Point(center.x,     center.y + 1),
                  Point(center.x + 1, center.y),
                  Point(center.x - 1, center.y + 1)]
        Shape.__init__(self, coords, 'green')
        self.center_block = self.blocks[0]
        self.shift_rotation_dir = True
        self.rotation_dir = -1

我似乎无法做到这一点。

真正掌握这一点的唯一方法是看看超类,
Shape
对它做了什么。如果它本身将其存储为一个属性,您就可以掌握它。

真正掌握它的唯一方法是看看超类
Shape
对它做了什么。如果它本身将其存储为一个属性,则您可以获得它。

在您的情况下,一种肮脏的黑客(您被唤醒)方式是wrap
Shape。\uuuu init\uuuu
方法并在以下范围内工作:

class Coords():
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Point(Coords):
    def __repr__(self):
        return '<Point ({} {})>'.format(self.x, self.y)

class Shape():
    def __init__(self, *args, **kw):
       pass

class S_shape(Shape):
    def __init__(self, center):
        coords = [Point(center.x,     center.y),
                  Point(center.x,     center.y + 1),
                  Point(center.x + 1, center.y),
                  Point(center.x - 1, center.y + 1)]
        Shape.__init__(self, coords, 'green')
        self.shift_rotation_dir = True
        self.rotation_dir = -1


def coordinates_logger(func):
    def wrapper(self, coords, color): # assume we need exactly first arg to __init__
        print coords          # access to coords, perform some needed action here
        self._coords = coords # for example store them
        return func(self, coords, color)
    wrapper.__name__ = func.__name__
    wrapper.__doc__ = func.__doc__
    wrapper.__dict__.update(func.__dict__)
    return wrapper

# monkey-patch superclass
Shape.__init__ =  coordinates_logger(Shape.__init__)

obj = S_shape(Coords(1,2)) 
# [<Point (1 2)>, <Point (1 3)>, <Point (2 2)>, <Point (0 3)>]
print obj._coords 
# [<Point (1 2)>, <Point (1 3)>, <Point (2 2)>, <Point (0 3)>]
class-Coords():
定义初始化(self,x,y):
self.x=x
self.y=y
课程积分(Coords):
定义报告(自我):
返回“”。格式(self.x,self.y)
类形状():
定义初始值(自,*args,**kw):
通过
S_类形状(形状):
定义初始(自我,中心):
坐标=[点(中心x,中心y),
点(中心x,中心y+1),
点(中心x+1,中心y),
点(中心点x-1,中心点y+1)]
形状。uuu初始(self,coords,'green')
self.shift\u rotation\u dir=True
self.rotation_dir=-1
def坐标记录仪(func):
def包装器(self、coords、color):#假设我们需要恰好第一个参数来初始化__
打印coords#访问coords,在此处执行一些必要的操作
self._coords=coords#例如,存储它们
返回函数(自身、坐标、颜色)
包装器.\uuuuu名称\uuuuu=func.\uuuuuu名称__
包装器.\uuuuu文档\uuuuu=func.\uuuuu文档__
包装器.目录更新(功能目录)
返回包装器
#猴斑超类
形状
obj=S_形(坐标系(1,2))
# [, , ]
打印对象
# [, , ]
在您的情况下,一种肮脏的骇客(您被唤醒)方式是包裹
形状

class Coords():
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Point(Coords):
    def __repr__(self):
        return '<Point ({} {})>'.format(self.x, self.y)

class Shape():
    def __init__(self, *args, **kw):
       pass

class S_shape(Shape):
    def __init__(self, center):
        coords = [Point(center.x,     center.y),
                  Point(center.x,     center.y + 1),
                  Point(center.x + 1, center.y),
                  Point(center.x - 1, center.y + 1)]
        Shape.__init__(self, coords, 'green')
        self.shift_rotation_dir = True
        self.rotation_dir = -1


def coordinates_logger(func):
    def wrapper(self, coords, color): # assume we need exactly first arg to __init__
        print coords          # access to coords, perform some needed action here
        self._coords = coords # for example store them
        return func(self, coords, color)
    wrapper.__name__ = func.__name__
    wrapper.__doc__ = func.__doc__
    wrapper.__dict__.update(func.__dict__)
    return wrapper

# monkey-patch superclass
Shape.__init__ =  coordinates_logger(Shape.__init__)

obj = S_shape(Coords(1,2)) 
# [<Point (1 2)>, <Point (1 3)>, <Point (2 2)>, <Point (0 3)>]
print obj._coords 
# [<Point (1 2)>, <Point (1 3)>, <Point (2 2)>, <Point (0 3)>]
class-Coords():
定义初始化(self,x,y):
self.x=x
self.y=y
课程积分(Coords):
定义报告(自我):
返回“”。格式(self.x,self.y)
类形状():
定义初始值(自,*args,**kw):
通过
S_类形状(形状):
定义初始(自我,中心):
坐标=[点(中心x,中心y),
点(中心x,中心y+1),
点(中心x+1,中心y),
点(中心点x-1,中心点y+1)]
形状。uuu初始(self,coords,'green')
self.shift\u rotation\u dir=True
self.rotation_dir=-1
def坐标记录仪(func):
def包装器(self、coords、color):#假设我们需要恰好第一个参数来初始化__
打印coords#访问coords,在此处执行一些必要的操作
self._coords=coords#例如,存储它们
返回函数(自身、坐标、颜色)
包装器.\uuuuu名称\uuuuu=func.\uuuuuu名称__
包装器.\uuuuu文档\uuuuu=func.\uuuuu文档__
包装器.目录更新(功能目录)
返回包装器
#猴斑超类
形状
obj=S_形(坐标系(1,2))
# [, , ]
打印对象
# [, , ]

不行,函数内部声明的变量不能在函数外部访问。最好将其定义为类或实例属性。问题是该类不是我的,我无法编辑它,我只能访问它。您确定“Shape”超类不保存坐标吗?给定名称,coords似乎是实例数据的一个重要部分。但是您可以查看Shape实例具有哪些字段:
dir(Shape(…)
。您希望查找实例的字段,而不是类的字段。不,您不能,函数内声明的变量不能在函数外访问。最好将其定义为类或实例属性。问题是该类不是我的,我无法编辑它,我只能访问它。您确定“Shape”超类不保存坐标吗?给定名称,coords似乎是实例数据的一个重要部分。但您可以查看形状实例的字段:
dir(Shape(…)
。您希望查找实例的字段,而不是类的字段。知道在您正在做的事情中,您可以始终使用monkey patch Shape来存储它们,您可以随时将其保存为猴子补丁形状