我有一个代码:
class ABC:
def test():
print('Hi there')
ABC.test()
其中产出:
Hi there
然而,这一做法失败了:
ABC().test()
TypeError: test() takes 0 positional arguments but 1 was given
# Makes sense
我们知道,当我们调用像classmethod这样的<classname>.<methodname>()时,类被隐式地作为参数传递给它,但是test这里不带参数。
为什么我们没有看到
我有两个方法,一个用于单个实例,另一个用于该类中的每个实例:
class MasterMatches(models.Model):
@classmethod
def update_url_if_any_matches_has_one(cls):
# apply to all instances, call instance method.
def update_url_if_any_matches_has_one(self):
# do something
我应该给它们起同样的名字吗?或者,这里一个好的命名约定是什么?
这就是我的代码
class InviteManager():
ALREADY_INVITED_MESSAGE = "You are already on our invite list"
INVITE_MESSAGE = "Thank you! we will be in touch soon"
@staticmethod
@missing_input_not_allowed
def invite(email):
try:
db.session.add(Invite(email))
我希望通过所有实例定义一个具有类范围/访问权限的数组。示例模块(bc.py):
class B:
glist = [] # array common to all instances
def __init__(self, number):
glist.append(number)
def printCurrentList():
我对python有点陌生,对线程非常陌生。我正在尝试为客户机- the服务器接口编写一个客户端,并且我使用的是状态设计模式。我被困在类中使用计时器对象作为类变量。我正在使用threading.Timer类实现该对象,但在将set_time方法作为参数传递给构造函数时遇到了麻烦。如果我使用threading.Timer(5.0,self.set_time),我会得到一个"NameError: name‘self“未定义的错误。如果我使用threading.Timer(5.0,set_time),我会得到一个"NameError: name 'set_time‘未定义“错误
我是python的新手(来自c++世界),曾经尝试过使用类方法。我创建了一个没有任何参数的方法(在本例中,有意避免使用self参数)。然后我试着把它叫做
class car:
def car_method():
print("Inside Car method")
obj = car_method()
obj.car_method() <---- this creates error: TypeError: car_method() takes 0 positional arguments but 1 was given
我认为这个错误
如果我有一个包含两个方法的父类:
class Parent():
@abstractmethod
@staticmethod
def functionA():
pass
def functionB():
return __class__.functionA() + 1
我实现了一个子类:
class Child(Parent):
def functionA(): # this function is different for each kind of child
return 3
最后,子类的目的是
我试图访问外部函数中的类变量,但是我得到了AttributeError,“类没有属性”--我的代码如下所示:
class example():
def __init__():
self.somevariable = raw_input("Input something: ")
def notaclass():
print example.somevariable
AttributeError: class example has no attribute 'somevariable'
其他的问题也被问到了类似的问题,然而
我想扩展Enum类并添加一个实用函数,该函数将被其他类使用。
考虑下面的代码:
from enum import Enum
class BaseEnum(Enum):
def get_items():
print(__class__)
return __class__.__members__.items()
class DerivedEnum(BaseEnum):
X = 1
Y = 2
print(BaseEnum.get_items())
print(DerivedEnum.get_items())
显然,这并不能像预期的那样工
我给你打电话
Hardware.gpio_active(True)
这是我的硬件课程:
import os
import glob
import time
import RPi.GPIO as GPIO
#class to manage hardware -- sensors, pumps, etc
class Hardware(object):
#global vars for sensor
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*
在ruby中,你可以这样做:
class A
def self.a
'A.a'
end
end
puts A.a #-> A.a
如何在python中做到这一点。我需要一个类的方法被调用,而不是在类的实例上被调用。当我尝试这样做时,我得到了这个错误:
unbound method METHOD must be called with CLASS instance as first argument (got nothing instead)
这是我尝试过的:
class A
def a():
return '
我知道类变量和实例变量的区别,以及它们应该驻留在我的代码中的位置,但是,下面是我感到困惑的地方。例如,如果我在实例变量中有一个元组列表,那么在for循环块中,分配给tuple值的变量是否应该具有“self”。前缀?
class Asdf():
cls_var = [(1,2), (3,4)]
def __init__(self):
self.ins_var = [(1,2), (3,4)]
def method(self):
for (x, y) in self.ins_var: # should it be this way
我是python,我了解到方法中的第一个参数应该包含一些' self‘关键字,但是我发现下面的程序运行时没有self关键字,您能解释一下下面的代码吗?
class Student(object):
def __init__(self,name,age):
self.name = name
self.age = age
def get_biggest_number(*age):
result=0
for item in age:
if item > result:
在PHP中,有一种访问在继承程序上定义/重写的静态属性值的方法。
例如:
class Foo {
public static $name='Foo';
public function who(){
echo static::$name;//the static operator
}
}
class Bar extends Foo {
public static $name='Bar';
}
$bar = new Bar();
$bar->who();
//Prints "Bar";
有什么办法用达特语做同样