小编Cau*_*uly的帖子

如何使用后面定义的类型进行函数注解?

问题是这样的:

class A():
    def foo() -> B:
        pass

class B():
    def bar() -> A:
        pass
Run Code Online (Sandbox Code Playgroud)

这将引发一个NameError: name 'B' is not defined.

为了进行类型检查,我不愿意更改-> B-> "B". 有什么解决方法吗?

python annotations python-3.x python-typing

7
推荐指数
2
解决办法
1265
查看次数

如何在Linux中使用包含非Ascii字符串的wchar_t*打开文件?

环境:Gcc/G ++ Linux

我在文件系统中有一个非ascii文件,我打算打开它.

现在我有一个wchar_t*,但我不知道如何打开它.(我信任的fopen只打开char*文件)

请帮忙.非常感谢.

c c++ linux file wchar

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

在调用函数之前,我可以延迟装饰器吗?

首先,我了解了Python装饰器是什么以及它们是如何工作的.我希望它能做到这样的事情:

def age_over_18(go_enjoy_yourself):

    def go_home_and_rethink_your_life():

        return 'So you should go home and rethink your life.'

    return go_enjoy_yourself if age_stored_somewhere > 18 else go_home_and_rethink_your_life

@age_over_18
def some_porn_things():

    return '-Beep-'
Run Code Online (Sandbox Code Playgroud)

但我发现装饰器是在Python首次读取函数时执行的,这意味着该函数实际上什么也不做.

我知道我可以这样写:

def some_porn_things():

    if age_stored_somewhere > 18:
        ...
    else:
        ...
Run Code Online (Sandbox Code Playgroud)

但我只是认为装饰者优雅且易于理解,所以问题是:

在调用函数之前,我可以延迟装饰器吗?

python decorator

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

如何获取静态方法的类名

所以基类中有一个静态方法,子类应该使用它。

但是,我需要知道哪个子类调用了静态方法。

代码是这样的:

class BaseClass():
    @staticmethod
    def getname():
        #some magic

class SubClassA(BaseClass): 
    pass

class SubClassB(BaseClass):
    pass

SubClassA.getname()   #hope to see 'SubClassA'
SubClassB.getname()   #hope to see 'SubClassB'
Run Code Online (Sandbox Code Playgroud)

或者,这甚至可能吗?

python static-methods

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