我在 VSCode 和 Code Runner 扩展中遇到了这个问题,以下是导致该问题的代码片段:
class init_error(Exception):
def __init__(self, comp="Internals"):
self.component = comp
self.message = f"Error occurred while initiating {self.component}"
def __str__(self):
return self.message
Run Code Online (Sandbox Code Playgroud)
我一开始以为是Python2和Python3之间的编译器搞错了,但在指定#!/usr/bin/env python3并检查是否print("foo")有效后,我相当确定这不是版本问题。我已检查 Code Runner 中的编译器设置为3.7.4 64-bit,因此我尝试通过扩展运行代码Python,并且它有效,所以我相信这是 Code Runner 问题。
很抱歉很长,但最后,f 字符串没有用红色下划线,并且不会出现问题,因此出于某种原因,f 字符串被认为是有效的语法,但不仅仅在代码中运行跑步者延伸。
如何让 Code Runner 接受 f 字符串?
感谢您的帮助。
PS 我认为这无关紧要,但我可以在一个月前发誓它有效。
python python-3.x visual-studio-code f-string vscode-code-runner
我有一段代码,如下所示:
import pandas as pd
file = pd.read_csv("/my/path/to/spreadsheet.csv", index_col=0)
file = file.dropna(how="any", inplace=True)
file = file.fillna("", inplace=False)
print(file)
Run Code Online (Sandbox Code Playgroud)
预期输出:
Profit ($) Spendings ($) Total Profit EOY Profit ($)
Month
Jan 200 80 120 3150
Feb 310 50 260
Mar 250 40 210
Apr 170 70 100
May 650 200 450
Jun 180 150 30
Jul 530 160 370
Aug 610 270 340
Sep 470 180 290
Oct 680 290 390
Nov 570 310 260
Dec 600 270 330
Run Code Online (Sandbox Code Playgroud)
电流输出:
回溯(最近一次调用最后):文件“/my/path/to/OpenSheet.py”,第5行,文件= …
我正在学习如何在Python中使用装饰器,并且对此有很深的了解,但是我有一个问题-为什么不能将装饰器用于内置函数?
为什么这样做:
def decorator1(func):
def inner(*args, **kwargs):
print("<>=========================<>")
func(*args, **kwargs)
print("<>=========================<>")
return inner
@decorator1
def greet():
print("Hello!")
greet()
Run Code Online (Sandbox Code Playgroud)
不是这个吗?:
def decorator1(func):
def inner(*args, **kwargs):
print("<>=========================<>")
func(*args, **kwargs)
print("<>=========================<>")
return inner
@decorator1
print("Hello!")
Run Code Online (Sandbox Code Playgroud)
是因为打印功能是在现场执行的,并且greet()仅在定义功能之后才运行@decorator1?