Opinion! Creating Template filters to work with class instance in templates, this works, wondering the most "django" optimal way?

huangapple go评论48阅读模式
英文:

Opinion! Creating Template filters to work with class instance in templates, this works, wondering the most "django" optimal way?

问题

在模板中访问类方法,这个方法是有效的,但我想知道是否有更好的方法?

someclass

class Something():
    somevar = None
    def __init__(self, somevar):
        self.somevar = somevar
    
    def set_somevar(self, newvar):
        self.somevar = newvar
    
    def set_weird_and_somevar(self, weird, somevar):
        self.weird = weird
        self.somevar = somevar
    
    def get_tag(self, tag):

templateTag

from django import template
register = template.Library()

@register.filter
def class_get_method(value, method):
    f = method.split('.')
    method = f[0]
    del f[0]
    p = getattr(value, method)
    if f:
        return p(*f)
    else:
        return p()

在模板中,假设content是一个类实例

{{content|class_get_method:'set_weird_and_somevar.wenday_adams.nothervar'}}
英文:

Accessing class methods in templates, this works but was wondering if their was a better way?

someclass

class Something():
    somevar = None
    def __init__(self, somevar):
        self.somevar = somevar
    
    def set_somevar(self, newvar):
        self.somevar = newvar
    
    def set_weird_and_somevar(self, weird, somevar):
        self.weird = weird
        self.somevar = somevar
    
    def get_tag(self, tag):
        
    

templateTag

from django import template
register = template.Library()

@register.filter
def class_get_method(value, method):
    f = method.split('.')
    method = f[0]
    del f[0]
    p = getattr(value, method)
    if f:
        return p(*f)
    else:
        return p()

in template, lets say content is a class instance

{{content|class_get_method:'set_weird_and_somevar.wenday_adams.nothervar'}}

答案1

得分: 1

class Something():
    somevar = None
    def __init__(self, somevar):
        self.somevar = somevar

哎呀!

不要这样做。

你描述的图灵机具有明确定义的语义。
但是Python工程师不会编写这样的代码,
因为这会导致维护问题。

Python是关于命名空间的。

有一个全局命名空间,Something 就在其中。

有一个类命名空间,自从 Something 类被定义(在解析时)以来,
有一个值为 Nonesomevar 属性。

随后,在运行时,我们创建了一对对象,
self.somevar 值为 12
但是类属性仍然是 None

这是完全明确定义的。
机器不会困惑。
但你或未来的维护工程师很可能会困惑。

请为类属性选择不同的名称。
请将其引用为 Something.somevar,或者在 @classmethod 中从 cls.somevar 引用它。


请注意,类属性 somevar 可以初始化为可变数据结构,例如字典。
然后,类方法和普通方法都可以对其进行更改。

英文:
class Something():
    somevar = None
    def __init__(self, somevar):
        self.somevar = somevar

Yikes!

Don't do that.

The Turing machine you describe has well-defined semantics.
But python engineers don't write such code,
because it leads to maintenance headaches.

Python is all about namespaces.

There is a global namespace, which Something is in.

There is a class namespace which,
ever since the Something class was defined (at parse time)
has a somevar attribute with value None.

Later on, at run time, we create a pair of objects
with self.somevar values of 1 and 2.
But the class attribute is still None.

This is perfectly well defined.
The machine won't become confused.
But you or future maintenance engineers very likely will.

Choose a different name for the class attribute, please.
Reference it as Something.somevar, or as cls.somevar
from within a @classmethod.


Notice that class attribute somevar can be initialized
as a mutable data structure, such as a dict.
And then both classmethods and ordinary methods can mutate it.

huangapple
  • 本文由 发表于 2023年2月6日 05:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定