我正在尝试找到一种方法将当前文件作为模块的参数。我在任何地方都找不到它,所以我正在尝试 stackoverflow
{
"name": "Python: Synapse",
"type": "python",
"request": "launch",
"module": "projFolder.targetModule",
"console": "integratedTerminal"
}
Run Code Online (Sandbox Code Playgroud)
手动指定模块参数(如上面的示例)是可行的。但考虑到对每个文件都这样做需要我将其自动化。我尝试过 ${file},但它给出的是文件路径而不是模块。所以没有成功。如何将当前文件作为模块启动?
我训练了两个独立的模型
我想要
not-related和good/normal/bad。我需要他们分开。我需要的一些伪代码
# Output of modelA will be a vector I presume `(1, None)` where `None` is batch
def ModelC.predict(input):
outputA = ModelA(input)
if outputA == 'not-related':
return outputA
return ModelB(outputA)
Run Code Online (Sandbox Code Playgroud)
我不知道如何if在模型推理中包含逻辑。我怎样才能做到这一点?
为什么我不能使用 walrus 运算符:=来分配属性?它在分配给局部变量时起作用:
my_eyes = ["left", "right"]
if saved_eye := my_eyes.index("left"):
print(saved_eye)
# outputs >>> 0
Run Code Online (Sandbox Code Playgroud)
但如果我尝试分配给对象属性,则会出现语法错误:
class MyEyes:
def __init__(self):
self.eyes = ["left", "right"]
self.saved_eye = None
def ohyes(self):
if self.saved_eye := self.eyes.index("left"):
print(self.saved_eye)
x = MyEyes()
x.ohyes()
# raises
# >>> if self.saved_eye := self.eyes.index("left"):
# >>> SyntaxError: cannot use assignment expressions with attribute
Run Code Online (Sandbox Code Playgroud)
我的意思是我可以使用临时局部变量绕过错误,但为什么会发生这种情况?我相信 100% 这是合法的语法。
出于纯粹的好奇,我做了以下事情。
class A:
def x():
print("A.x")
class B:
def x():
print("B.x")
class C(A,B):
pass
C.x()
Run Code Online (Sandbox Code Playgroud)
哪些输出
A.x
Run Code Online (Sandbox Code Playgroud)
现在我期望的行为是执行后来的超类的方法B.x。因为我认为 for 的方法A将首先定义而不是B覆盖它们的方法。相反,A.x被打印出来。事实上,第一个超类的方法.x总是被调用。实例方法显示了相同的行为。
谁能解释一下这是怎么回事?
我正在与一些带有pygame的射弹一起工作,并发现即使只有200行代码,游戏的运行速度也低于50 fps.(没有一个大的循环,除了运行循环,我的PC是相当新的)
那么,这是因为pygame使用SDL吗?
如果是这样,使用像OpenGL这样的GPU会提高性能吗?
Main.py
#Eemport
import pygame as pyg,sys,background, player, wall
from pygame.locals import *
#Screen
screen_size_width = 1280
screen_size_height = 720
screen_size = (screen_size_width,screen_size_height)
#Initialization
pyg.init()
screen = pyg.display.set_mode(screen_size)
pyg.display.set_caption("Collision and better physics")
#Set clock
Clock = pyg.time.Clock()
#Set Background
Background = background.background("beach.jpg",screen_size)
#Set Player
player_size_width = 128
player_size_height = 128
player_location_x = 640
player_location_y = 360
player_size = (player_size_width,player_size_height)
player_location = (player_location_x,player_location_y)
player_speed = 5
Player = player.player("crab.png",player_size,player_location)
#Set input
keys = {'right': False, 'left': False, 'up': …Run Code Online (Sandbox Code Playgroud) 我正在通过继承来制作数据集类torch.utils.data.Dataset,并遇到了以下问题。
与以前的函数不同,它返回固定类型的值,__getitem__但不会。例如,
class MyExample:
def __init__(self, some_list: list[int]):
self.some_list = some_list
def __getitem__(self, index):
return self.some_list[index]
Run Code Online (Sandbox Code Playgroud)
MyExample[<index>]将返回int,同时MyExample[<slice>]将返回slice of int。VScode智能感知自动T_co为其类型注释编写,但我不明白这意味着什么。
我不明白为什么
import std.core; import std;import std.iostream;#include <iostream>你能解释为什么会发生上述情况吗?也许我猜std.iostream不是一个模块。那为什么1.有效呢?
@Someprogrammerdue 提供了这个参考,它说
import <iostream>; // import declaration
Run Code Online (Sandbox Code Playgroud)
当我在编译器中运行以下时
import <iostream>;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到
main.cpp:1:8: error: 'iostream' was not declared in this scope
1 | import<iostream>;
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
这才进入我的脑海。
class Parent:
pass
class Child(Parent):
def __init__(self):
# is this necessary?
super().__init__()
Run Code Online (Sandbox Code Playgroud)
当一个类继承一个空类时,子类是否需要初始化它,为什么?
我运行了以下代码
vector<int> randomIntegers = generateIntegers(10); // Generates 10 integers
std::ranges::sort(randomIntegers);
Run Code Online (Sandbox Code Playgroud)
当我用 编译时g++ -std=c++20 file.cpp,我得到
error: 'sort' is not a member of 'std::ranges'; did you mean 'std::sort'?
Run Code Online (Sandbox Code Playgroud)
gcc --version:海湾合作委员会10.2.0g++ --version:g++10.2.0为什么 sort 不是成员?advance我正在使用 VScode intellisense,它显示了、begin、等方法common_view。但不排序。
要求
问题
-会降低外观(这些值不会对齐)f"{myfloat:+}"给出所有值的符号。但是,然后我不能使用str.ljust方法f"{str(myfloat:+).ljust(10)}",这当然引发了错误# My current code
values = [-1.0, 2.2]
for value in values:
formatted = str(value).ljust(10)
print(f"someLjustedStrings{formatted}someLjustedStrings")
#Output
#someLjustedStrings-1.0 someLjustedStrings
#someLjustedStrings2.2 someLjustedStrings
#Expected Output (best)
#someLjustedStrings-1.0 someLjustedStrings
#someLjustedStrings+2.2 someLjustedStrings
#OR
#someLjustedStrings-1.0 someLjustedStrings
#someLjustedStrings 2.2 someLjustedStrings
Run Code Online (Sandbox Code Playgroud) class Session:
@staticmethod
def load_from_json(json_path:str) -> Session:
pass
Run Code Online (Sandbox Code Playgroud)
上面抛出一个NameError: name 'Session' is not defined. 在其定义中使用自身作为类型提示是不可能的吗?
python ×9
python-3.x ×4
c++ ×2
c++20 ×2
type-hinting ×2
class ×1
formatting ×1
g++10 ×1
gcc ×1
inheritance ×1
keras ×1
pygame ×1
python-3.9 ×1
pytorch ×1
std-ranges ×1
string ×1