有人可以向我解释@classmethod
和@staticmethod
python中的含义吗?我需要知道差异和意义.
据我所知,@classmethod
告诉一个类,它是一个应该继承到子类的方法,或者......某种东西.但是,重点是什么?为什么不在不添加@classmethod
或定义@staticmethod
任何@
定义的情况下定义类方法?
tl; dr: 我应该何时使用它们,为什么要使用它们,我应该如何使用它们?
我在C++方面非常先进,所以使用更高级的编程概念应该不是问题.如果可能的话,请随意给我一个相应的C++示例.
类方法和实例方法有什么区别?
实例方法是访问器(getter和setter),而类方法几乎是其他所有方法吗?
在Ruby中,如何从该类的实例中调用类方法?说我有
class Truck
def self.default_make
# Class method.
"mac"
end
def initialize
# Instance method.
Truck.default_make # gets the default via the class's method.
# But: I wish to avoid mentioning Truck. Seems I'm repeating myself.
end
end
Run Code Online (Sandbox Code Playgroud)
该行Truck.default_make
检索默认值.但有没有提到这样说的方法Truck
呢?好像应该有.
我正在教自己Python,我最近的教训是Python不是Java,因此我花了一些时间将所有的Class方法转换为函数.
我现在意识到我不需要使用Class方法来处理static
Java中的方法,但现在我不确定何时使用它们.我能找到的关于Python类方法的所有建议都是像我这样的新手应该避开它们,而标准文档在讨论时最不透明.
有没有人有一个在Python中使用Class方法的好例子,或者至少可以有人告诉我什么时候可以明智地使用Class方法?
在python中,我可以使用@classmethod
装饰器向类添加方法.是否有类似的装饰器向类中添加属性?我可以更好地展示我在说什么.
class Example(object):
the_I = 10
def __init__( self ):
self.an_i = 20
@property
def i( self ):
return self.an_i
def inc_i( self ):
self.an_i += 1
# is this even possible?
@classproperty
def I( cls ):
return cls.the_I
@classmethod
def inc_I( cls ):
cls.the_I += 1
e = Example()
assert e.i == 20
e.inc_i()
assert e.i == 21
assert Example.I == 10
Example.inc_I()
assert Example.I == 11
Run Code Online (Sandbox Code Playgroud)
我上面使用的语法是可能的还是需要更多的东西?
我想要类属性的原因是我可以延迟加载类属性,这似乎足够合理.
如果您尝试以元编程方式创建类方法,这将非常有用:
def self.create_methods(method_name)
# To create instance methods:
define_method method_name do
...
end
# To create class methods that refer to the args on create_methods:
???
end
Run Code Online (Sandbox Code Playgroud)
我的回答是......
如何获取类成员函数的函数指针,然后使用特定对象调用该成员函数?我想写:
class Dog : Animal
{
Dog ();
void bark ();
}
…
Dog* pDog = new Dog ();
BarkFunction pBark = &Dog::bark;
(*pBark) (pDog);
…
Run Code Online (Sandbox Code Playgroud)
另外,如果可能的话,我也想通过指针调用构造函数:
NewAnimalFunction pNew = &Dog::Dog;
Animal* pAnimal = (*pNew)();
Run Code Online (Sandbox Code Playgroud)
这是可能的,如果是这样,那么首选的方法是什么?
请考虑以下代码:
class Base(object):
@classmethod
def do(cls, a):
print cls, a
class Derived(Base):
@classmethod
def do(cls, a):
print 'In derived!'
# Base.do(cls, a) -- can't pass `cls`
Base.do(a)
if __name__ == '__main__':
d = Derived()
d.do('hello')
> $ python play.py
> In derived!
> <class '__main__.Base'> msg
Run Code Online (Sandbox Code Playgroud)
从Derived.do
,我该如何打电话Base.do
?
super
如果这是一个普通的对象方法,我通常会直接使用甚至是基类名,但显然我找不到在基类中调用classmethod的方法.
在上面的例子中,Base.do(a)
print Base
类而不是Derived
类.
在集成我之前没有使用的Django应用程序时,我发现了两种不同的方法来定义类中的函数.作者似乎非常故意地使用它们.第一个是我自己经常使用的一个:
class Dummy(object):
def some_function(self,*args,**kwargs):
do something here
self is the class instance
Run Code Online (Sandbox Code Playgroud)
另一个是我不使用的,主要是因为我不明白何时使用它,以及为什么:
class Dummy(object):
@classmethod
def some_function(cls,*args,**kwargs):
do something here
cls refers to what?
Run Code Online (Sandbox Code Playgroud)
在Python文档中,classmethod
装饰器用这句话解释:
类方法接收类作为隐式的第一个参数,就像实例方法接收实例一样.
所以我猜cls
是指Dummy
自己(class
不是实例).我不完全理解为什么会这样,因为我总能做到这一点:
type(self).do_something_with_the_class
Run Code Online (Sandbox Code Playgroud)
这只是为了清楚起见,还是我错过了最重要的部分:如果没有它,那些怪异而迷人的东西是无法完成的?
python language-details method-dispatch class-method language-lawyer
我正在尝试学习Python中的super()函数.
我以为我掌握了它,直到我看到这个例子(2.6)并发现自己卡住了.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 9, in do_something
do_something = classmethod(do_something)
TypeError: unbound method do_something() must be called with B instance as first argument (got nothing instead)
>>>
Run Code Online (Sandbox Code Playgroud)
当我在示例之前读到这一行时,这不是我的预期:
如果我们使用类方法,我们没有一个实例来调用super.幸运的是,对于我们来说,super甚至可以使用类型作为第二个参数.---类型可以直接传递给super,如下所示.
通过说do_something()应该用B的实例调用,这正是Python告诉我的不可能.
class-method ×10
python ×6
class ×2
oop ×2
ruby ×2
c++ ×1
methods ×1
object ×1
objective-c ×1
overriding ×1
properties ×1
super ×1