mirror of
https://github.com/ytdl-org/youtube-dl.git
synced 2024-11-18 08:16:10 +00:00
[utils] Add classpropinit()
decorator for easier use of inherited class vars
This commit is contained in:
parent
e6c95bd192
commit
d0762cf36a
1 changed files with 30 additions and 0 deletions
|
@ -6540,3 +6540,33 @@ class classproperty(object):
|
||||||
elif cls not in self._cache:
|
elif cls not in self._cache:
|
||||||
self._cache[cls] = self.func(cls)
|
self._cache[cls] = self.func(cls)
|
||||||
return self._cache[cls]
|
return self._cache[cls]
|
||||||
|
|
||||||
|
|
||||||
|
class classpropinit(classproperty):
|
||||||
|
""" A Python fubar: parent class vars are not in scope when the
|
||||||
|
`class suite` is evaluated, so disallowing `childvar = fn(parentvar)`.
|
||||||
|
Instead, the parent class has to be mentioned redundantly and
|
||||||
|
unmaintainably, since the current class isn't yet bound.
|
||||||
|
This decorator evaluates a class method and assigns its result
|
||||||
|
in place of the method.
|
||||||
|
|
||||||
|
class child(parent):
|
||||||
|
# before
|
||||||
|
childvar = fn(parent.parentvar)
|
||||||
|
# now
|
||||||
|
@classpropinit
|
||||||
|
def childvar(cls):
|
||||||
|
return fn(cls.parentvar)
|
||||||
|
# or
|
||||||
|
childvar = classpropinit(lambda cls: fn(cls.parentvar))
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, func):
|
||||||
|
functools.update_wrapper(self, func)
|
||||||
|
self.name = func.__name__
|
||||||
|
self.func = func
|
||||||
|
|
||||||
|
def __get__(self, _, cls):
|
||||||
|
val = self.func(cls)
|
||||||
|
setattr(cls, self.name, val)
|
||||||
|
return val
|
||||||
|
|
Loading…
Reference in a new issue