标签: class-variables

Javascript 中类变量的等价物是什么?

编辑:感谢@Aadit M Shah 的确认。

function Apple () {}
Apple.someVar = null; // Class variable
Apple.prototype.someVar = null; // Instance variable
Run Code Online (Sandbox Code Playgroud)

javascript class instance-variables instance class-variables

3
推荐指数
1
解决办法
1万
查看次数

Python dataclasses.dataclass 引用变量而不是实例变量

c1 和 c2 的构造函数中的默认值应该为 b 和 b 生成新的实例变量。相反,看起来 c1.a 和 c2.a 正在引用相同的变量。@dataclass 是否创建了一个类变量?这似乎与预期的功能不一致,我在文档中找不到关于类变量的任何信息。所以,我认为这是一个错误。有人可以向我解释如何修复它吗?我应该将其报告为 python 跟踪器上的错误吗?

我知道这个问题必须与 python 按引用传递对象和按值传递内置类型的方式有关,因为 b 属性(它只是一个浮点数)显示了预期/期望的行为,而 a 属性(这是一个用户定义的对象)只是一个引用。

谢谢!

从数据类导入数据类

"""输入"""

@dataclass
class VS:
    v: float  # value
    s: float  # scale factor
    
    def scaled_value(self):
        return self.v*self.s

@dataclass
class Container:
    a: VS = VS(1, 1)
    b: float = 1

c1 = Container()
c2 = Container()

print(c1)
print(c2)

c1.a.v = -999
c1.b = -999

print(c1)
print(c2)
Run Code Online (Sandbox Code Playgroud)

"""输出"""

Container(a=VS(v=1, s=1), b=1)
Container(a=VS(v=1, s=1), b=1)
Container(a=VS(v=-999, s=1), b=-999)
Container(a=VS(v=-999, …
Run Code Online (Sandbox Code Playgroud)

python instance-variables class-variables

3
推荐指数
1
解决办法
1394
查看次数

java单例模式,应该所有变量都是类变量吗?

如果一个类实现了单例模式,那么所有变量都应该声明为static吗?

有什么理由不应该被宣布为静态吗?这有什么不同吗?

java singleton static design-patterns class-variables

2
推荐指数
1
解决办法
7928
查看次数

从类变量引用静态方法

我知道有这样的情况是有线的但不知怎的,我有它:

class foo
  #static method
  @staticmethod
  def test():
    pass

  # class variable
  c = {'name' : <i want to reference test method here.>}
Run Code Online (Sandbox Code Playgroud)

它的方法是什么?

仅供记录:

我认为这应该被视为python最差的做法.如果有的话,使用静态方法并不是真正的pythoish方式......

python static-methods class-variables

2
推荐指数
1
解决办法
455
查看次数

在Ruby中有没有更好的方法来运行class_eval()来提取类变量?

我个人对此没有任何反对意见,除了这个事实很长,但真正困扰我的是这个词eval.

我在JavaScript中做了很多东西,我从类似于eval的任何东西中运行,就像它是魔鬼一样,我也不喜欢参数是一个字符串的事实(再次,可能是因为它是eval).

我知道我可以编写自己的方法来修复方法名称长度问题,我的'方法名称问题'和参数 -as -a-string thingy,但我真正想知道的是:有更好,更短,发烧友,但原生,class_eval提取类变量的方式?

旁注:我知道 class_variable_get() 的存在class_variables(),但它们看起来并不吸引我; 非常长,不是吗?

编辑:更新问题更具体.

谢谢!

ruby class-variables

2
推荐指数
1
解决办法
223
查看次数

继承类中的Ruby和类变量

class A
  def set(v)
    @@v = v
  end
  def put
    puts @@v
  end
end

class B < A
end
class C < A
end

B.new.set 'b'
B.new.put # => b
C.new.set 'c'
C.new.put # => c
B.new.put # => c
Run Code Online (Sandbox Code Playgroud)

为什么?我怎么写这个在最后B.new.put中有'b'?

ruby class-variables

2
推荐指数
1
解决办法
3112
查看次数

Python - 在派生类定义中附加到类级别列表

class A (object):
    keywords = ('one', 'two', 'three')

class B (A):
    keywords = A.keywords + ('four', 'five', 'six')
Run Code Online (Sandbox Code Playgroud)

有没有什么办法改变A.keywords<thing B derives from>.keywords,有点像super(),但预__init__/self?我不喜欢在定义中重复类名.

用法:

>>> A.keywords
('one', 'two', 'three')
>>> B.keywords
('one', 'two', 'three', 'four', 'five', 'six')
Run Code Online (Sandbox Code Playgroud)

python class-variables

2
推荐指数
1
解决办法
497
查看次数

Python类名作为类变量

我正在使用类和子类作为应用程序.对于每个类,super和sub,都有一个名为的变量类label.我希望label超类的变量默认为类名.例如:

class Super():
    label = 'Super'

class Sub(Super):
    label = 'Sub'
Run Code Online (Sandbox Code Playgroud)

而不是手动为每个类输出变量,是否可以从超类中的类名派生变量并自动为子类填充?

class Super():
    label = # Code to get class name

class Sub(Super)
    pass
    # When inherited Sub.label == 'Sub'.
Run Code Online (Sandbox Code Playgroud)

原因是这将是默认行为.我也希望如果我能获得默认行为,我可以稍后通过指定备用行为来覆盖它label.

class SecondSub(Super):
    label = 'Pie'  # Override the default of SecondSub.label == 'SecondSub'
Run Code Online (Sandbox Code Playgroud)

我尝试过使用__name__,但这不起作用,只是给了我'__main__'.

我想label@classmethod方法中使用类变量.所以我希望能够在不必实际创建Super()或Sub()对象的情况下引用该值,如下所示:

class Super():
    label = # Magic

    @classmethod
    def do_something_with_label(cls):
        print(cls.label)
Run Code Online (Sandbox Code Playgroud)

python class-variables python-3.x

2
推荐指数
3
解决办法
1781
查看次数

Python Unittest-类变量

我希望有人不介意解释这里发生了什么.我试图运行一个已经确认使用python 2.7工作的python单元测试.但是,当试图在运行python 2.6的机器上运行相同的测试时,我收到一个我无法弄清楚的错误.这是一个正在发生的事情的例子

import re, string, os, subprocess, unittest
import MERCH_FUNCTIONS


class merchTests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self._merchFileString=open("test_file.txt",'r').read()
        self._merchFileList=self._merchFileString.split("\n") #convert string to list

    def test_stuff(self):
         #print list
         print(self._merchFileList) 
if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

出于某种原因,如果我使用python 2.7运行此代码,它会成功运行测试,并打印出self._merchFileList列表.

但是,当使用python 2.6运行相同的代码时,我收到以下错误:

======================================================================
ERROR: test_stuff (__main__.merchTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "MERCH_Test_Case.py", line 14, in test_stuff
    print(self._merchFileList)
AttributeError: 'merchTests' object has no attribute '_merchFileList'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
Run Code Online (Sandbox Code Playgroud)

我不能为我的生活弄清楚这里发生了什么.我尝试了几个不同的事情但没有成功.如果有人愿意解释这里出了什么问题,我将非常感激.

先感谢您.

python unit-testing python-2.6 class-variables python-2.7

2
推荐指数
1
解决办法
2348
查看次数

用self调用类变量

你怎么想出这个有趣的(至少对我而言)的例子.

import numpy as np


class Something(object):
    a = np.random.randint(low=0, high=10)

    def do(self):
        self.a += 1
        print(self.a)

if __name__ == '__main__':
    something = Something()
    print(something.__str__())
    something.do()
    something2 = Something()
    print(something2.__str__())
    something2.do()
    something3 = Something()
    print(something3.__str__())
    something3.do()
Run Code Online (Sandbox Code Playgroud)

以上内容在控制台中打印以下内容:

$ python test.py
<__main__.Something object at 0x7f03a80e0518>
1
<__main__.Something object at 0x7f03a80cfcc0>
1
<__main__.Something object at 0x7f03a80cfcf8>
1
Run Code Online (Sandbox Code Playgroud)

我有点困惑,因为我(错误地)认为价值a会增加.

如果我使用@classmethod装饰器,我能够获得我期望的行为.

import numpy as np


class Something(object):
    a = np.random.randint(low=0, high=10)

    @classmethod
    def do(cls):
        cls.a += 1
        print(cls.a)

if __name__ …
Run Code Online (Sandbox Code Playgroud)

python instance-variables class-variables python-3.x

2
推荐指数
1
解决办法
1709
查看次数