首先,如果我问一些愚蠢的话,我很抱歉,因为我是Python的新手...
我正在阅读http://docs.python.org/3.1/reference/datamodel.html#objects-values-and -types并看到那句话:
type()函数返回一个对象的类型(它本身就是一个对象)
当然,我决定检查一下:
>>> def someFunction(x):
... return x * x
...
>>> type(someFunction)
<class 'function'>
>>> type(type)
<class 'type'>
Run Code Online (Sandbox Code Playgroud)
所以,看起来函数有function类型,但是type如果它是一个函数,为什么函数有不同的类型?或者说文档是撒谎,它不是真正的功能?
我目前正在研究一个简单的基于文本的Python游戏,只是为了练习python和面向对象的编程,但我遇到了这个错误,它告诉我'LargeManaPotion'没有属性'name',当我看到它确实如此,它的声明方式与'SmallManaPotion'完全相同.我假设这是一个愚蠢的错误,我只是在俯视或者什么但是会很感激帮助.此外,当我在player.inventory函数中打印播放器的库存时,程序将打印药水,所以我不确定为什么它在交易功能中不起作用.无论如何,这是相关的代码.提前致谢.
class ManaPotion:
def __init__(self):
raise NotImplementedError("Do not create raw ManaPotion objects.")
def __str__(self):
return "{} (+{} Mana)".format(self.name, self.mana_value)
class LargeManaPotion(ManaPotion):
def __init__(self):
self.name = "Large Mana Potion"
self.mana_value = 45
self.value = 40
class SmallManaPotion(ManaPotion):
def __init__(self):
self.name = "Small Mana Potion"
self.mana_value = 15
self.value = 10
Run Code Online (Sandbox Code Playgroud)
如您所见,它与SmallManaPotion相同.这是导致错误的函数.
class TraderTile(MapTile):
def intro_text(self):
return "A frail not-quite-human, not-quite-creature squats in the corner " \
"\nclinking his gold coins together. \nHe looks willing to trade."
def __init__(self, x, y):
self.trader …Run Code Online (Sandbox Code Playgroud) 我编写了一些代码,使我能够从 DICOM 文件中获取数据并将数据分离为单独的 FID 信号。
from pylab import *
import dicom
import numpy as np
import scipy as sp
plan=dicom.read_file("1.3.46.670589.11.38085.5.22.3.1.4792.2013050818105496124")
all_points = array(plan.SpectroscopyData)
cmplx_data = all_points[0::2] -1j*all_points[1::2]
frames = int(plan.NumberOfFrames)
fid_pts = len(cmplx_data)/frames
del_t = plan.AcquisitionDuration / (frames * fid_pts)
fid_list = []
for fidN in arange(frames):
offset = fidN * fid_pts
current_fid = cmplx_data[offset:offset+fid_pts]
fid_list.append(current_fid)
Run Code Online (Sandbox Code Playgroud)
我现在想量化其中一些数据,因此在应用傅立叶变换和移位后,我尝试使用 Scipy 的四元函数并遇到以下错误:
spec = fftshift(fft(fid_list[0])))
sp.integrate.quad(spec, 660.0, 700.0)
error Traceback (most recent call last)
/home/dominicc/Experiments/In Vitro/Glu, Cr/Phantom 1: 10mM Cr, 5mM Glu/WIP_SV_PRESS_ME_128TEs_5ms_spacing_1828/<ipython-input-107-17cb50e45927> in …Run Code Online (Sandbox Code Playgroud) 我正在测试simpleTAL模板库,它使用callable来测试传入模板的对象是否是函数.可调用的定义表示如果一个对象包含魔术方法,则该对象是可调用的__call__.另请参见Python中的"可调用"是什么?.
但是,使用以下类定义创建的对象(python 2.7.4)
class H:
def __init__(self, val):
self.a = val
def __getattr__(self, name):
return 'blah'
h = H(1)
callable(h)
Run Code Online (Sandbox Code Playgroud)
将返回True.但是,如果__getattr__引发了AttributeError,系统地没有意义,它将返回False!
有人可以对这个问题有所了解并可能解决方案吗?(我不希望h可以调用).
我觉得这是一个非常简单的问题,但我找不到合适的答案.基本上我有一个名为simulation的类调用函数列表:
simulation.addGroup("teapotarmy")
simulation.populateGroup(20)
simulation.addNode("input",INPUT)
simulation.addNode("output",OUTPUT);
simulation.connectNodes("input","output");
simulation.manipOutputNode("output", "group.xvel");
simulation.manipInputNode("input", 1, 0.05);
Run Code Online (Sandbox Code Playgroud)
有没有办法调用这些函数而不必每次都重复类名?沿线的东西:
(thethingIwant) simulation:
addGroup("teapotarmy")
populateGroup(20)
addNode("input",INPUT)
...
Run Code Online (Sandbox Code Playgroud)
我用其他编程语言完成了这个,但我还没有弄清楚Python中的语法.我有一个微弱的记忆,它与'with'声明有关...?提前致谢.
莱昂