小编tyl*_*eax的帖子

字典值作为访问密钥时要调用的函数,而不使用"()"

我有一个字典,其值有时作为字符串,有时作为函数.对于作为函数的值,有一种方法可以在()访问密钥时执行函数而无需显式键入?

例:

d = {1: "A", 2: "B", 3: fn_1}
d[3]() # To run function
Run Code Online (Sandbox Code Playgroud)

我想要:

d = {1: "A", 2: "B", 3: magic(fn_1)}
d[3] # To run function
Run Code Online (Sandbox Code Playgroud)

python dictionary function callback python-2.7

11
推荐指数
2
解决办法
4781
查看次数

谷歌字体显示=交换奇怪的行为

我试图通过将这段代码注入我的 html 代码来使用 google font api

<link href="https://fonts.googleapis.com/css?family=Playfair+Display|Source+Serif+Pro|Suwannaphum&display=swap" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

我发现在加载我的网站时,使用该字体的文本会出现“故障”。它会在短时间内显示为小,然后变为正常。

经过大量试验后,我&display=swap从代码中删除了部分,问题不再发生,我能够使用该字体。

为什么会&display=swap导致故障?我直接从google api字体网站复制了代码。

html css fonts google-api google-fonts

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

OrderedDict 如何在 Python 中保持秩序

我很好奇集合库中的 OrderedDict如何保持密钥/对顺序?我在网上看了一圈,找不到答案。

python dictionary ordereddictionary data-structures

4
推荐指数
1
解决办法
175
查看次数

Maven项目中的哪些文件应提交给git?

我想知道Maven项目中的哪些文件应提交给git。

我是否应该mvn clean在提交之前执行,还是在.gitignore文件中添加某些文件?

java apache git maven mvn-repo

3
推荐指数
3
解决办法
7410
查看次数

如何将文件结构表示为Python对象

我正在尝试找到一种将文件结构表示为 python 对象的方法,这样我就可以轻松获得特定路径,而不必键入所有字符串。这适用于我的情况,因为我有一个静态文件结构(不变)。

我想我可以将目录表示为类,将目录中的文件表示为类/静态变量。

我希望能够浏览 python 对象,以便它返回我想要的路径,即:

print(FileStructure.details.file1) # root\details\file1.txt
print(FileStructure.details) # root\details
Run Code Online (Sandbox Code Playgroud)

我从下面的代码中得到的是:

print("{0}".format(FileStructure())) # root
print("{0}".format(FileStructure)) # <class '__main__.FileStructure'>
print("{0}".format(FileStructure.details)) # <class '__main__.FileStructure.details'>
print("{0}".format(FileStructure.details.file1)) # details\file1.txt
Run Code Online (Sandbox Code Playgroud)

到目前为止我拥有的代码是......

import os 

class FileStructure(object): # Root directory
    root = "root"

    class details(object): # details directory
        root = "details"
        file1 = os.path.join(root, "file1.txt") # File in details directory
        file2 = os.path.join(root, "file2.txt") # File in details directory

        def __str__(self):
            return f"{self.root}"

    def __str__(self):
        return f"{self.root}"
Run Code Online (Sandbox Code Playgroud)

我不想必须实例化该类才能完成这项工作。我的问题是:

  1. 如何调用类对象并让它返回字符串而不是 < class ....> 文本
  2. 如何让嵌套类使用其父类?

python object python-3.x

3
推荐指数
1
解决办法
1697
查看次数

有没有办法将相同的值传递给函数的所有参数?

我想知道是否有一种方法可以在不知道可用参数的情况下将值传递给函数的所有参数?

例子:

def SomeFunction(arg1, arg2, arg3):
    print(arg1)
    print(arg2)
    print(arg3)

SomeFunction("Hi"**)

>> Hi
>> Hi
>> Hi
Run Code Online (Sandbox Code Playgroud)

**这种情况下仅仅是占位符语法。

python python-3.x

3
推荐指数
1
解决办法
80
查看次数