python类方法第一个参数_你能在python类的__init__中使用静态方法作为默认参数吗?(Can you use a static method as default parameter i...

The reason you’re having problems with your static method usage as a default argument is due to a combination of two issues.

The first issue is that the default argument needs to be well defined when the

1def

statement is run, not only when the function is called. That’s because the default argument gets built into the function object, rather than being recalculated each time the function runs (this is the same reason why a mutable default argument like an empty list is often an error). Anyway, this is why you can’t use

1MyClass.static_method

as the default argument, since

1MyClass

isn’t defined yet when the function is being defined (the class object is only made after all its contents have been created).

The next issue is that a

1staticmethod

object doesn’t have all the same attributes and methods as a regular function. Normally this doesn’t matter, as when you access it through a class object (e.g.

1MyClass.static_method

once

1MyClass

exists) or through an instance (e.g.

1self.static_method

), it will be callable and have a

1__name__

. But that’s because you get the underlying function in those situations, rather than the

1staticmethod

object itself. The

1staticmethod

object itself is a descriptor, but not a callable.

So neither of these functions will work correctly:

1

2

3

4

5

6

7

8

9

10

11class MyClass:

@staticmethod

def static_method():

pass

def foo(self, func=MyClass.static_method): # won't work because MyClass doesn't exist yet

pass

def bar(self, func=static_method): # this declaration will work (if you comment out foo)

name = func.__name__ # but this doesn't work when the bar() is called

func() # nor this, as func is the staticmethod object

What does work would be to use the actual function underlying the

1staticmethod

object as the default:

1

2

3def baz(self, func=static_method.__func__): # this works!

name = func.__name__

func()

This also works when you pass in some other function (or bound method), unlike the version of your code that used

1name = func.__func__.__name__

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值