如何在静态方法中获取类的名称,我有继承并想要派生类的名称
在下面的示例中,my_name()方法中的XXX应该有什么替代方法
class snake()
@staticmethod
def my_name():
print XXX.__name___
class python (snake)
pass
class cobra (snake)
pass
python.my_name()
# I want output to be python
cobra.my_name()
# I want output to be cobra
我在C++中定义了一个使用Boost向Python公开的C++类。我的脚本应该从这个类派生出来,每当定义了一个新的子类时,我想要进行一些初始化。
如何设置公开的Event类的元类,以便每当Python脚本从该类派生出来时,元类就可以完成所需的初始化?
我想避免在脚本中显式地使用元类.
class KeyboardEvent(Event): # This is what I want
pass
class KeyboardEvent(Event, metaclass=EventMeta): # This is not a good solution
pass
编辑:解决方案
为什么python 2和python 3中的代码输出是不同的?
class A:
def m(self):
print("m of A called")
class B(A):
pass
class C(A):
def m(self):
print("m of C called")
class D(B,C):
pass
x = D()
x.m()
实际产出:
$ python diamond1.py //python 2 used for the code
m of A call
class A:
def __init__(self):
print 'A'
class B(A):
def __init__(self):
print 'B'
b = B()
B
在C++中,我期望看到A B输出,但在Python中,我只能看到B。我知道我可以用super(B, self).__init__()在Python语言中实现同样的效果,但由于这显然不是默认的(或者这是-我也是语法新手),我担心初始化对象的范例完全不同。
那么,Python中的对象是什么,它们与类有什么关系,在Python中初始化
这是
我跟踪了许多线程,它们表明问题coid存在于form类的验证器中,但我检查了fo中类的them..heres代码。
1.下面是来自forms.py的代码
from flask_wtf import Form
from wtforms import StringField, SubmitField, BooleanField, PasswordField
from wtforms.validators import DataRequired, Length, email, EqualTo, Required
class RegistrationForm(Form):
usern
我想知道,虽然继承可以用于继承派生类中的属性和方法。
为什么我们不在派生类中编写函数,而将它们声明为覆盖。
下面的例子将解释更多关于我的问题。
class Net
{
public virtual void Act()
{
}
}
class Perl : Net
{
public override void Act()
{
Console.WriteLine("Perl.Act");
}
}
class Python : Net
{
public override void Act()
{
C
在Python中支持自定义类的关键字参数解包需要什么?在Python2.7和Python3.6解释器中,尝试解压缩不兼容类型时都会出现以下错误消息:
>>> dict(**None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type object argument after ** must be a mapping, not NoneType
映射是否意味着对象必须指定为collections.Mapping
我想在我用python创建的类上使用堆队列():
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
我想按年龄比较堆队列中的狗。我如何告诉python按年龄比较我的对象?换句话说,我可以在python中以某种方式写一个“比较器”吗?
我正在尝试使用pythonnet将静态c#类传递到python中。我可以使用类似于下面示例的scope.Set("person",pyPerson)。然而,在我的例子中,这是一个实用程序(静态)类,我得到错误:在下面的示例中,util不包含testfn。 using Python.Runtime;
// create a person object
Person person = new Person("John", "Smith");
// acquire the GIL before using the Python interpret
编辑:下面是当前的问题:
Traceback (most recent call last): File "figure.py", line 32, in <module> import Python2
File "C:\scripts\python2.py", line 12, in <module> shp = shape.Python1(self.size, self.length, self.breadth,self.height)
NameError: name 'self' is not defined
我在python中尝试了NVI(非虚拟接口)习惯用法,并注意到私有(双下划线)方法似乎并不是虚拟的。
class A(object):
def a(self):
print "in A.a"
self.b()
self.__b()
self._b()
def _b(self):
print "in A._b"
def __b(self):
print "in A.__b"
def b(self):
print "in A.b"
class B(A):
我想在python中实现单例模式,我喜欢中描述的模式。
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class SingletonClass(metaclass=S
我读过
我试着从这个例子中复制上元类,发现这并不是在所有情况下都有效的:
def upper(cls_name, cls_parents, cls_attr):
""" Make all class attributes uppper case """
attrs = ((name, value) for name, value in cls_attr.items()
问题
我在Python3.3.4中观察到了一些我想要帮助理解的行为:为什么当函数正常执行时,而不是在工人池中执行函数时,我的异常会被正确地引发?
代码
import multiprocessing
class AllModuleExceptions(Exception):
"""Base class for library exceptions"""
pass
class ModuleException_1(AllModuleExceptions):
def __init__(self, message1):