我有一个字典,其值有时作为字符串,有时作为函数.对于作为函数的值,有一种方法可以在()访问密钥时执行函数而无需显式键入?
例:
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) 我试图通过将这段代码注入我的 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字体网站复制了代码。
我很好奇集合库中的 OrderedDict如何保持密钥/对顺序?我在网上看了一圈,找不到答案。
我想知道Maven项目中的哪些文件应提交给git。
我是否应该mvn clean在提交之前执行,还是在.gitignore文件中添加某些文件?
我正在尝试找到一种将文件结构表示为 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)
我不想必须实例化该类才能完成这项工作。我的问题是:
我想知道是否有一种方法可以在不知道可用参数的情况下将值传递给函数的所有参数?
例子:
def SomeFunction(arg1, arg2, arg3):
print(arg1)
print(arg2)
print(arg3)
SomeFunction("Hi"**)
>> Hi
>> Hi
>> Hi
Run Code Online (Sandbox Code Playgroud)
在**这种情况下仅仅是占位符语法。
python ×4
dictionary ×2
python-3.x ×2
apache ×1
callback ×1
css ×1
fonts ×1
function ×1
git ×1
google-api ×1
google-fonts ×1
html ×1
java ×1
maven ×1
mvn-repo ×1
object ×1
python-2.7 ×1