相关疑难解决方法(0)

我们可以拥有静态虚函数吗?如果没有,那么为什么?

可能重复:
C++静态虚拟成员?

我们可以拥有静态虚函数吗?如果没有,那么为什么?

class X
{
public:
       virtual static void fun(){} // Why we cant have static virtual function in C++?
};
Run Code Online (Sandbox Code Playgroud)

c++ static-methods virtual-functions

54
推荐指数
2
解决办法
6万
查看次数

classmethod在这段代码中做了什么?

在django.utils.tree.py中:

def _new_instance(cls, children=None, connector=None, negated=False):
        obj = Node(children, connector, negated)
        obj.__class__ = cls
        return obj
    _new_instance = classmethod(_new_instance)
Run Code Online (Sandbox Code Playgroud)

我不知道classmethod这段代码中的内容是什么.有人可以解释它的作用以及如何使用它吗?

python

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

Python - 我应该把我的帮助函数放在类的内部还是外部?

在Python中,如果类的某些方法需要辅助函数,但是辅助函数本身不使用类中的任何东西,我应该将辅助函数放在类的内部还是外部?

我试过把它放在里面,但是PyLint抱怨说这个功能可以放在外面.

@Karl:

该类是软件升级程序,如果该文件夹尚不存在,则辅助函数会创建一个新文件夹.这个类在一个模块中,现在几乎只有类的代码.其他课程可能会在以后添加.

python class pylint

46
推荐指数
2
解决办法
2万
查看次数

静态方法如何在Python中访问类变量?

这就是我的代码

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))
            db.session.commit()
        except IntegrityError:
            return ALREADY_INVITED_MESSAGE
        return INVITE_MESSAGE
Run Code Online (Sandbox Code Playgroud)

当我运行我的测试时,我明白了

NameError: global name 'INVITE_MESSAGE' is not defined
Run Code Online (Sandbox Code Playgroud)

我怎样才能进入INVITE_MESSAGE里面@staticmethod

python

45
推荐指数
4
解决办法
2万
查看次数

Python:避免关于太多参数的pylint警告

我想将一个大的Python函数重构为较小的函数.例如,请考虑以下代码段:

x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
Run Code Online (Sandbox Code Playgroud)

当然,这是一个微不足道的例子.在实践中,代码更复杂.我的观点是它包含许多必须传递给提取函数的局部范围变量,它们可能如下所示:

def mysum(x1, x2, x3, x4, x5, x6, x7, x8, x9):
    x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
    return x
Run Code Online (Sandbox Code Playgroud)

问题是pylint会触发有关太多参数的警告.做以下事情可以避免警告:

def mysum(d):
    x1 = d['x1']
    x2 = d['x2']
    ...
    x9 = d['x9']
    x = x1 + x2 + x3 + x4 + x5 + x6 + x7 …
Run Code Online (Sandbox Code Playgroud)

python refactoring pylint

44
推荐指数
8
解决办法
7万
查看次数

为什么使用classmethod而不是staticmethod?

我知道他们做了什么,我已经看到了很多这两个例子,但我没有找到一个例子,我必须使用classmethod而不是用一个替换它staticmethod.

classmethod我见过的最常见的例子是创建一个类本身的新实例,就像这样(非常简化的例子,没有使用方法atm.但你明白了):

class Foo:
    @classmethod
    def create_new(cls):
        return cls()
Run Code Online (Sandbox Code Playgroud)

这会Foo在调用时返回一个新实例foo = Foo.create_new().现在为什么我不能只使用它:

class Foo:
    @staticmethod
    def create_new():
        return Foo()
Run Code Online (Sandbox Code Playgroud)

它完全相同,我为什么要使用classmethoda staticmethod

python static-methods class class-method python-3.x

26
推荐指数
2
解决办法
2458
查看次数

如何让Django信号调用模型方法?

也许现在已经很晚了,但我无法弄清楚为什么这不起作用.当我有一个post_save信号调用泛型函数时,它可以工作,但是当我有一个post_save信号从模型中调用一个方法时,没有任何反应.这是有效的代码:

class Revision(models.Model):
    # Model junk...

def send_email(sender, instance, created, **kwargs):
    if created:
        print "DO STUFF"

signals.post_save.connect(send_email, sender=Revision)
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

class Revision(models.Model):
    # Model junk...

    def send_email(sender, instance, created, **kwargs):
        if created:
            print "DO STUFF"

signals.post_save.connect(Revision.send_email, sender=Revision)
Run Code Online (Sandbox Code Playgroud)

那里有一个善良的灵魂会阻止我把头砸到墙上吗?谢谢.

python django django-signals django-models

20
推荐指数
1
解决办法
7857
查看次数

从静态方法访问静态变量

我想从静态方法访问静态变量:

#!/usr/bin/env python
class Messenger:
    name = "world"
    @staticmethod
    def get_msg(grrrr):
        return "hello " + grrrr.name

print Messenger.get_msg(Messenger)
Run Code Online (Sandbox Code Playgroud)

没有传递grrrr给方法怎么做?这是真正的OOP吗?

任何喜欢nameself.name似乎不起作用的东西:

NameError: global name 'name' is not defined
Run Code Online (Sandbox Code Playgroud)

NameError: global name 'self' is not defined
Run Code Online (Sandbox Code Playgroud)

python oop static-methods static-variables

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

我们真的需要python中的@staticmethod装饰器来声明静态方法

我很好奇为什么我们需要@staticmethod装饰器将方法声明为static.我正在阅读Python中的静态方法,我开始知道静态方法可以在不实例化其类的情况下进行调用.所以我尝试了下面的两个例子,但两者都做了同样的事情:

class StatMethod:
  def stat():
    print("without Decorator")

class StatMethod_with_decorator:
  @staticmethod
  def stat():
    print("With Decorator")
Run Code Online (Sandbox Code Playgroud)

如果我stat()直接在类上调用该方法,则打印/显示以下值:

>> StatMethod.stat()
without Decorator
>> StatMethod_with_decorator.stat()
With Decorator
Run Code Online (Sandbox Code Playgroud)

python static

17
推荐指数
1
解决办法
7769
查看次数

Python静态方法,为什么?

可能重复:
Python中@staticmethod和@classmethod有什么区别?

我在课堂上有一些关于staticmethods的问题.我将首先举一个例子.

例一:

class Static:
    def __init__(self, first, last):
        self.first = first
        self.last = last
        self.age = randint(0, 50)
    def printName(self):
        return self.first + self.last
    @staticmethod
    def printInfo():
        return "Hello %s, your age is %s" % (self.first + self.last, self.age)

x = Static("Ephexeve", "M").printInfo()
Run Code Online (Sandbox Code Playgroud)

输出:

Traceback (most recent call last):
  File "/home/ephexeve/Workspace/Tests/classestest.py", line 90, in <module>
    x = Static("Ephexeve", "M").printInfo()
  File "/home/ephexeve/Workspace/Tests/classestest.py", line 88, in printInfo
    return "Hello %s, your age is %s" % (self.first + self.last, self.age)
NameError: …
Run Code Online (Sandbox Code Playgroud)

python oop attributes static-methods class-method

11
推荐指数
1
解决办法
4001
查看次数