The example below is a no frills version of something I saw out in the wild.
class Client(object):
def __init__(self, session):
self.session = session
def _get(self, path, **kwargs):
self.session.get("etc")
def ScopedObject(cls):
return cls
ScopedClient = ScopedObject(Client) # Why??
What is the use of the last line?
Here is an example I tried out to make sense of what happening. (Code above is simply for context).
>>> class A:
... def __init__(self, v):
... self.x = v
...
>>> def scope(cls): return cls
...
>>> a = A
>>> b = scope(a)
>>> a
<class __main__.A at 0x10989c2f0>
>>> b
<class __main__.A at 0x10989c2f0>
>>> a(1)
<__main__.A instance at 0x1098df910>
>>> b(1)
<__main__.A instance at 0x1098df870>
>>> a(1)
<__main__.A instance at 0x1098df910>
So, okay it causes their instances to be different. But I could have accomplished the same thing by doing:
>>> c = a
>>> c(1)
<__main__.A instance at 0x1098df870>
Which returns the exact same instance as b
.
What am I missing?
Aucun commentaire:
Enregistrer un commentaire