有时需要在Python中检查参数.例如,我有一个函数接受网络中其他节点的地址作为原始字符串地址或接收封装其他节点信息的类Node.
我使用type(0函数,如:
if type(n) == type(Node):
do this
elif type(n) == type(str)
do this
Run Code Online (Sandbox Code Playgroud)
这是一个很好的方法吗?
更新1: Python 3具有函数参数的注释.这些可用于使用工具进行类型检查:http://mypy-lang.org/
我是Python新手,我想在控制台中安装Jupyter Notebook,然后输入以下内容:
pip3 install --upgrade pip
Run Code Online (Sandbox Code Playgroud)
之后,我在使用pip3安装其他库时出错,控制台输出:
File "/usr/bin/pip3", line 11, in <module>
sys.exit(main())
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么办。
我用完sudo autoremove python3-pip之后sudo apt install python3-pip
我正在尝试遍历加载程序以检查它是否正常工作,但是给出了以下错误:
TypeError: img should be PIL Image. Got <class 'torch.Tensor'>
我试着将两者transforms.ToTensor()并transforms.ToPILImage()和它给我一个错误,要求相反。即,使用ToPILImage(),它将要求张量,反之亦然。
# Imports here
%matplotlib inline
import matplotlib.pyplot as plt
from torch import nn, optim
import torch.nn.functional as F
import torch
from torchvision import transforms, datasets, models
import seaborn as sns
import pandas as pd
import numpy as np
data_dir = 'flowers'
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'
#Creating transform for training set
train_transforms = …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚这个非常简单的片段有什么问题:
class A(object):
def printme(self):
print "A"
self.printme()
a = A()
Run Code Online (Sandbox Code Playgroud)
回溯(最近一次调用最后一次):文件"prog.py",第1行,在A类(对象)中:文件"prog.py",第5行,在A self.printme()中NameError:名称'self'不是定义
我曾经BigDecimal.setScale(7, RoundingMode.HALF_UP)将数字四舍五入到 7 个小数位,但是现在如果我得到一个没有任何小数位的数字或它们小于 7,我会0在数字中得到无用的's,例如在以40这种方式四舍五入之后我会得到40.0000000.
是否可以使用SetScale并同时去除无意义的 0将数字舍入一定数量的小数位?
我最近开始学习 C++,我有一个关于我们讲座中关于声明不同类型变量时的准确性的练习的语法问题,在这种情况下是float和double。
#include <iostream>
using namespace std ;
int main()
{
// Accuracy test with float
float eps_f = 1.0 ;
while (float(1.0 + eps_f) != 1.0)
eps_f /= 2.0 ;
cout << "Resolving capacity float approximately: " << 2*eps_f << endl ;
// Accuracy test with double
double eps_d = 1.0 ;
while (1.0 + eps_d != 1.0)
eps_d /= 2.0 ;
cout << "Resolving capacity double approximately : " << 2*eps_d << endl ; …Run Code Online (Sandbox Code Playgroud) 这是我的最小可重现示例:
#include <stdio.h>
int main( int argc, char* argv[])
{
printf (" this is the contents of argc:%d\n",argc);
int i;
for (i = 0; i < argc ; i++){
printf(" argv = %d = %s\n",i,argv[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我argc将 for 循环更改为数字时,比方说10,代码在到达之前崩溃10:
$ ./argc one two three
this is the contents of argc:4
argv = 0 = ./argc
argv = 1 = one
argv = 2 = two
argv = 3 = three
argv …Run Code Online (Sandbox Code Playgroud) 我在编写测试代码的一些单元测试中使用函数装饰器。但是,我发现这个装饰器会导致函数被调用两次(因此打印其输出两次)。
在某些函数返回错误的返回值后,我发现了这个错误,这种情况只有在调用函数两次时才会发生。
#!/usr/bin/env python3
def decorate(func):
@wraps(func)
def inner(*args, **kwargs):
print("#" * 40)
print("Testing function {}".format(func.__name__))
print("Arguments passed: {} ".format(args))
print("Begin output of {}".format(func.__name__))
print("#" * 40)
try:
func(*args, **kwargs)
except Exception as e:
print("Error occured: {}".format(e))
print("#" * 40)
print("End of output of {}".format(func.__name__))
print("#" * 40)
print("\n" * 5)
return func(*args,**kwargs) #Error happens on this line here
return inner
#Add decorator to function definition.
@decorate
def asdf():
print("THIS SHOULD PRINT ONCE")
#Call function
asdf()
Run Code Online (Sandbox Code Playgroud)
输出(间距与复制的完全相同):
########################################
Testing function …Run Code Online (Sandbox Code Playgroud) 样例Bash脚本:
#!/bin/bash
echo $0
echo $1
Run Code Online (Sandbox Code Playgroud)
作为运行
$ ./prog foo
Run Code Online (Sandbox Code Playgroud)
将打印:
./prog
foo
Run Code Online (Sandbox Code Playgroud)
因此,我知道如何在Shell程序中获取位置参数。
我想弄清楚的是如何对其他任何shell命令执行相同的操作。像mv,例如,我想能说
mv file1 .file1.bak
Run Code Online (Sandbox Code Playgroud)
但我不想两次都不必输入file1。
我尝试了与上面的shell脚本相同的方法。
$ echo a b c $0 $1 $2
Run Code Online (Sandbox Code Playgroud)
但是这个印
a b c -zsh
Run Code Online (Sandbox Code Playgroud)
我以为这可能只是特定于Shell的内容,因此我在中运行了它bash。
a b c bash
Run Code Online (Sandbox Code Playgroud)
TL; DR:如何从扩展中的Shell命令获取位置参数?
tuple = ("Hi", "i am", "new")
letter = input("What letter would you like to find?")
if letter in tuple:
for x in letter:
print(x)
Run Code Online (Sandbox Code Playgroud)
我在输出中没有得到任何东西。
这个想法是要求一个字母,然后打印元组中包含该字母的单词。