简体   繁体   English

在python中获取对象的父命名空间?

[英]Getting object's parent namespace in python?

In python it's possible to use '.' 在python中,可以使用'。' in order to access object's dictionary items. 为了访问对象的字典项。 For example: 例如:

class test( object ) :
  def __init__( self ) :
    self.b = 1
  def foo( self ) :
    pass
obj = test()
a = obj.foo

From above example, having 'a' object, is it possible to get from it reference to 'obj' that is a parent namespace for 'foo' method assigned? 从上面的例子中,拥有'a'对象,是否有可能从它引用'obj',它是'foo'方法分配的父命名空间? For example, to change obj.b into 2? 例如,将obj.b更改为2?

On bound methods, you can use three special read-only parameters: 在绑定方法上,您可以使用三个特殊的只读参数:

  • im_func which returns the (unbound) function object im_func返回(未绑定)函数对象
  • im_self which returns the object the function is bound to (class instance) im_self返回函数绑定的对象(类实例)
  • im_class which returns the class of im_self im_class它返回类本身

Testing around: 测试周围:

class Test(object):
    def foo(self):
        pass

instance = Test()
instance.foo          # <bound method Test.foo of <__main__.Test object at 0x1>>
instance.foo.im_func  # <function foo at 0x2>
instance.foo.im_self  # <__main__.Test object at 0x1>
instance.foo.im_class # <__main__.Test class at 0x3>

# A few remarks
instance.foo.im_self.__class__ == instance.foo.im_class # True
instance.foo.__name__ == instance.foo.im_func.__name__  # True
instance.foo.__doc__ == instance.foo.im_func.__doc__    # True

# Now, note this:
Test.foo.im_func != Test.foo # unbound method vs function
Test.foo.im_self is None

# Let's play with classmethods
class Extend(Test):
    @classmethod
    def bar(cls): 
        pass

extended = Extend()

# Be careful! Because it's a class method, the class is returned, not the instance
extended.bar.im_self # <__main__.Extend class at ...>

There is an interesting thing to note here, that gives you a hint on how the methods are being called: 这里有一个有趣的事情需要注意,它提供了如何调用方法的提示:

class Hint(object):
    def foo(self, *args, **kwargs):
        pass

    @classmethod
    def bar(cls, *args, **kwargs):
        pass

instance = Hint()

# this will work with both class methods and instance methods:
for name in ['foo', 'bar']:
    method = instance.__getattribute__(name)
    # call the method
    method.im_func(method.im_self, 1, 2, 3, fruit='banana')

Basically, im_self attribute of a bound method changes, to allow using it as the first parameter when calling im_func 基本上,绑定方法的im_self属性会发生变化,允许在调用im_func时将其用作第一个参数

Python 2.6+ (including Python 3) Python 2.6+(包括Python 3)

You can use the __self__ property of a bound method to access the instance that the method is bound to. 您可以使用绑定方法__self__属性来访问方法绑定的实例。

>> a.__self__
<__main__.test object at 0x782d0>
>> a.__self__.b = 2
>> obj.b
2

Python 2.2+ (Python 2.x only) Python 2.2+(仅限Python 2.x)

You can also use the im_self property, but this is not forward compatible with Python 3. 您也可以使用im_self属性,但这与Python 3不兼容。

>> a.im_self
<__main__.test object at 0x782d0>

since python2.6 synonyms for im_self and im_func are __self__ and __func__ , respectively. 因为im_selfim_func python2.6同义词分别是__self____func__ im* attributes are completely gone in py3k. im*属性在py3k中完全消失了。 so you would need to change it to: 所以你需要将它改为:

>> a.__self__
<__main__.test object at 0xb7b7d9ac>
>> a.__self__.b = 2
>> obj.b
2

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

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