你可以添加新的语句(例如print,raise,with)Python的语法?
说,允许..
mystatement "Something"
Run Code Online (Sandbox Code Playgroud)
要么,
new_if True:
print "example"
Run Code Online (Sandbox Code Playgroud)
如果你应该,而不是如果它是可能的(没有修改python解释器代码)
我是Python的新手并且遵循教程.list教程中有一个例子:
example = list('easyhoss')
Run Code Online (Sandbox Code Playgroud)
现在,在教程中,example= ['e','a',...,'s'].但在我的情况下,我得到以下错误:
>>> example = list('easyhoss')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
请告诉我我错在哪里.我搜索了这个,但它是不同的.
我有以下类与变量from,to和rate.from是一个关键字.如果我想在下面的init方法中使用它,那么编写它的正确方法是什么?
更多上下文:类需要from显式变量,因为它是由另一个开发人员用不同语言编写的POST端点所需的json的一部分.因此,改变变量名是不可能的.
class ExchangeRates(JsonAware):
def __init__(self, from, to, rate):
self.from = from
self.to = to
self.rate = rate
Run Code Online (Sandbox Code Playgroud)
JsonAware代码:
class PropertyEquality(object):
def __eq__(self, other):
return (isinstance(other, self.__class__) and self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, ', '.join(['%s=%s' % (k, v) for (k, v) in self.__dict__.items()]))
class JsonAware(PropertyEquality):
def json(self):
return json.dumps(self, cls=GenericEncoder)
@classmethod
def from_json(cls, json):
return cls(**json)
Run Code Online (Sandbox Code Playgroud)
GenericEncoder代码:
class GenericEncoder(json.JSONEncoder):
def …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的Python脚本来导出,导入和区分数据库.我想让用户提供他们想要运行脚本的"模式",我选择import,export和diff作为我的选项.当我通过argparse运行它时,所有解析的选项都以args结尾,我可以使用arg.export或args.diff访问它们,但由于"import"是一个关键字,我遇到了问题.
我可以做一些改变的工作,让它工作,但我想知道是否可以保留我拥有的东西.例如,我可以将选项缩短为"exp","imp"和"diff",或者我可以做一个名为"mode"的选项,希望传入"import","export"或"diff".
我目前的代码:
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("--export", help="Export source(s)", action="store_true")
group.add_argument("--import", help="Import source(s)", action="store_true")
group.add_argument("--diff", help="Diff sources", action="store_true")
parser.add_argument("filename", help="XML Filename used for exporting to, importing from or comparing while doing diff.")
args = parser.parse_args()
if args.export:
export_sources(args.filename)
elif args.import:
import_sources(args.filename)
elif args.diff:
diff_sources(args.filename)
Run Code Online (Sandbox Code Playgroud) 因此,我已经对下划线字符(_)进行了一些研究。我知道它的大多数用例及其语义,因此我将它们放到下面作为回顾,最后我将得出一个问题,该问题更多是关于两个用例的概念性问题。
gettext别名为_在十进制分组中以提高可见性(特别是进行3组分组,例如1_000_000)- 请注意,仅从Python 3.6开始可用。
例:
1_000_000 == 10**6 # equals True
x = 1_000
print(x) # yields 1000
Run Code Online (Sandbox Code Playgroud)为了“忽略”某些值,尽管我不会将其称为“忽略”,因为这些值仍会_像常规标识符一样进行评估和绑定。通常,我会找到比这更好的设计,因为我发现这是一种代码味道。多年来,我很少使用这种方法,因此,我想每当您认为需要使用它时,肯定可以改进设计以不使用它。
例:
for _ in range(10):
# do stuff without _
ping_a_server()
# outside loop that still exists and it still has the last evaluated value
print(_) # would yield 9
Run Code Online (Sandbox Code Playgroud)尾随一个标识符(习惯上是为了避免名称与内置标识符或保留字冲突):
例
class Automobile:
def __init__(self, type_='luxury', class_='executive'):
self.car_type = type_
self.car_class …Run Code Online (Sandbox Code Playgroud)