动态更改Python类属性

时间:2015-06-09 15:09:20

标签: python class django-admin

我有一个继承A类的B类,其类属性为cls_attr。 我想在B类中动态设置cls_attr。 这样的事情:

.cd-top {
 display: inline-block;
 height: 40px;
 width: 40px;
 position: fixed;
 bottom: 100px;
 right: 10px;
 z-index: 10;
 box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
 /* image replacement properties */
 overflow: hidden;
 text-indent: 100%;
 white-space: nowrap;
 background: rgba(232, 98, 86, 0.8) url(../img/cd-top-arrow.svg) no-repeat   center 50%;
 visibility: hidden;
 opacity: 0;
 -webkit-transition: opacity .3s 0s, visibility 0s .3s;
 -moz-transition: opacity .3s 0s, visibility 0s .3s;
 transition: opacity .3s 0s, visibility 0s .3s; 
 }
.cd-top.cd-is-visible, .cd-top.cd-fade-out, .no-touch .cd-top:hover {
-webkit-transition: opacity .3s 0s, visibility 0s 0s;
-moz-transition: opacity .3s 0s, visibility 0s 0s;
transition: opacity .3s 0s, visibility 0s 0s; 
}
.cd-top.cd-is-visible {
/* the button becomes visible */
visibility: visible;
opacity: 1;
}
.cd-top.cd-fade-out {
/* if the user keeps scrolling down, the button is out of focus and becomes    less visible */
opacity: .5;
}
.no-touch .cd-top:hover {
 background-color: #e86256;
 opacity: 1;
 }
 @media only screen and (min-width: 768px) {
 .cd-top {
 right: 20px;
 bottom: 20px;
  }
 }
@media only screen and (min-width: 1024px) {
.cd-top {
height: 60px;
width: 60px;
right: 30px;
bottom: 30px;
 }
}

我尝试了几件事。我知道我可能不会在正确的地方寻找,但我没有解决方案。

编辑:课程是django管理员课程

感谢。

4 个答案:

答案 0 :(得分:4)

可以在类或实例上读取类属性,但是只能在类上设置它们(尝试在实例上设置它们只会创建一个会影响类属性的实例属性)。

如果条件在导入时已知,您只需在class正文中进行测试:

xxx = True 

Class A(object):
   cls_attr= 'value'

Class B(A):
   if xxx:
       cls_attr = 'this_value'
   else
       cls_attr = 'that_value'

现在,如果您想在程序执行期间更改它,您必须使用classmethod

class B(A):
   @classmethod
   def set_cls_attr(cls, xxx):   
       if xxx:
           cls.cls_attr = 'this_value'
       else
           cls.cls_attr = 'that_value'

或者如果您需要在测试期间访问您的实例:

class B(A):
   def set_cls_attr(self, xxx):   
       cls = type(self)
       if xxx:
           cls.cls_attr = 'this_value'
       else
           cls.cls_attr = 'that_value'

答案 1 :(得分:2)

如何使用classmethod并在子类中多态覆盖它?

class A:
    @classmethod
    def cls_attr(cls):
        return 'value'

class B(A):
    @classmethod
    def cls_attr(cls):
        if cond():
            return 'this'
        else:
            return 'that'

assert A.cls_attr() == 'value'      
cond = lambda: True
assert B.cls_attr() == 'this'
cond = lambda: False
assert B.cls_attr() == 'that'

答案 2 :(得分:0)

对我来说,最简单的解决方案是使用property装饰器:

class B:
    @property
    def attr_name(self):
        """ do your stuff to define attr_name dynamically """
        return attr_name

答案 3 :(得分:-2)

这似乎可以做你想要的:

>>> class B(A):
     @classmethod
     def set_cls_val(cls, x):
             if x == 1:
                     cls.cls_attr = "new"

>>> c = B()
>>> c.cls_attr
'value'
>>> c.set_cls_val(B, 1)
>>> c.cls_attr
'new'
>>> B.cls_attr
'new'

只需在功能中设置它。

编辑:更新以设置class属性而不是实例属性,感谢@ bruno-desthuilliers。

编辑:再次更新,感谢@ bruno-desthuilliers。我应该更清楚地思考我的答案。但是你想要的是下面的答案。