English 中文(简体)
如何在第3.7号决议的3.7段重新插入沙丘__qualname__?
原标题:How to reimplement Python s __qualname__ in Python 3.7? (with some minor adjustments)

<>代码>_qualname__因地制宜而使我受益;然而,由于:

  1. 。 在我使用的情况下,我需要提及母物体。

  2. 有时返回<代码>超标/代码> 不属于参考类别。 例如:

    class Parent():
    
        def __init__(self):
            pass
    
    
    class Child(Parent):
        pass
    
    
    print(Child.__init__.__qualname__)  # Prints: "Parent.__init__"
    
  3. 我正在开发的一揽子计划需要坚固,最突出的例子有:_qualname_。 http://www.python.org/dev/peps/pep-3155/“rel=“nofollow noreferer”>。 只要我能说的话。

>parsing ́s with ast, qualname<_/code> be relemented in Zhu__3 with visit? 如何执行<代码>_qualname__? 在执行核心功能时,我认为我能够加以调整,以适应我的使用情况。


<>Prior Research:

我无法在沙捞越源法中找到执行标语。

最佳回答

你不会得到你想要的东西。 请注意:your_thing(Parent.__init__),就Parent your_thing(Child.__init__),就Child,但Parent.__init>和Child.__init_均为同一对象。

你用两种不同的方式来查阅,但灰色没有这方面的记录。 无论你执行什么,都只能得到功能标的,而没有你要求的信息。

即使你做一些可怕的ack检查,看看<代码>your_thing(Child.__init__)的源码在其中说“Child”,在功能存放于变数或经过几层功能电话时,就赢得了工作。 仅仅因为你在撰写法典时已经掌握了你想要的信息,就一小部分案件而言,这毫无道理。

问题回答

如果您能够接受使用<代码>Base node的子类的非常规(或累))级分类,则每个属性

class A(BaseNode):
    class a(BaseNode):
        pass
    a = a()


class B(BaseNode):
    class a1(A):
        pass
    a1 = a1()

    class a2(A):
        pass
    a2 = a2()


class C(B):
    class c(BaseNode):
        pass
    c = c()

那么,每个归属者都知道他在级别上的道路名称。 (可通过使用<条码>_init_subclass__(或代号)来简化分类。)

c = C()
assert c._pathname      ==  C 
assert c.c._pathname    ==  C.c 
assert c.a1._pathname   ==  C.a1 
assert c.a1.a._pathname ==  C.a1.a 
assert c.a2._pathname   ==  C.a2 
assert c.a2.a._pathname ==  C.a2.a 

每个属性案例都是根据您可在<条码><>/条码>产出上看到的要求设立的。

*** <__main__.C object at 0x7fca81558190>  creates  c  from <class  __main__.C.c >
*** <__main__.C object at 0x7fca81558190>  creates  a1  from <class  __main__.B.a1 >
*** <__main__.B.a1 object at 0x7fca8155b760>  creates  a  from <class  __main__.A.a >
*** <__main__.C object at 0x7fca81558190>  creates  a2  from <class  __main__.B.a2 >
*** <__main__.B.a2 object at 0x7fca8155b490>  creates  a  from <class  __main__.A.a >

    class BaseNode:
    def __init__(self, parent=None):
        self._parent = parent

    @property
    def _path(self):
        path = []
        obj = self
        while obj:
            path.append(obj)
            obj = obj._parent
        return path[::-1]

    @property
    def _pathname(self):
        return  . .join(node.__class__.__name__ for node in self._path)

    def __get__(self, obj, cls=None):
        if obj is None:
            return self
        name = self.__class__.__name__
        try:
            inst = obj.__dict__[name]
        except KeyError:
            print(f *** {obj}  creates {name!r} from {self.__class__} )
            inst = self.__class__(obj)
            obj.__dict__[name] = inst
        return inst

The descriptor method __get__() dynamically creates the attributes (= BaseNode instances) on demand.

BTW:欢迎对这种做法的任何评论或改进:





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签