简体   繁体   English

Python 2.7与3.x中的函数

[英]Functions in Python 2.7 vs 3.x

I'm new to Python. 我是Python的新手。 I've been trying to familiarize myself with Numpy, Scipy, and Matplotlib, as I have a background in the sciences, and hope to make myself a more competitive candidate for work in neuroscience laboratories. 我一直想让自己熟悉Numpy,Scipy和Matplotlib,因为我具有科学背景,并希望使自己成为神经科学实验室工作中更具竞争力的候选人。

I've been browsing through the Matplotlib documentation, trying to learn by example. 我一直在浏览Matplotlib文档,尝试通过示例进行学习。 I will reference an example from the following URL: http://matplotlib.org/users/pyplot_tutorial.html 我将从以下URL引用示例: http : //matplotlib.org/users/pyplot_tutorial.html

I am under the impression that these examples are written in Python 3.x, and that I am having trouble because I am using Python 2.7. 我觉得这些示例是用Python 3.x编写的,因为我使用的是Python 2.7,所以遇到了麻烦。 I am using 2.7 because some of the libraries I wanted weren't available for 3.x. 我使用的是2.7,因为我想要的某些库不适用于3.x。

The website gave an example of using subplots. 该网站提供了一个使用子图的示例。 Their code is as follows: 他们的代码如下:


import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')

This is supposed to return a Figure like this: http://i.stack.imgur.com/ejNDu.png 应该返回这样的图形: http : //i.stack.imgur.com/ejNDu.png


When I copy the same code into IDLE, it gives me an error. 当我将相同的代码复制到IDLE时,它给我一个错误。 On the line 在线上

t1 = np.arange(0.0, 5.0, 0.1)

, IDLE tells me that "t1" is invalid syntax. ,IDLE告诉我“ t1”是无效的语法。

My first question: What is the problem with using t1 as a variable? 我的第一个问题:使用t1作为变量有什么问题?

If I copy in similar code, but with a few things tweaked, I can avoid this error. 如果我以类似的代码进行复制,但做了一些调整,则可以避免此错误。 However, I am then presented with another error. 但是,然后我遇到另一个错误。 When I add the line equivalent to 当我添加等效于

plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

, IDLE presents me with the error: ,IDLE向我显示错误:


Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    plt.plot(ty, f(tx), 'bo', ty, f(ty), 'k')
NameError: name 'f' is not defined

I'm not sure how Python expects me to define "f" separately from the function "f(t)". 我不确定Python如何期望我与函数“ f(t)”分开定义“ f”。

My second and MAIN question: Could somebody please explain why "f" must be defined separately? 我的第二个主要问题:有人可以解释为什么必须单独定义“ f”吗? How do I use functions like this correctly in Python 2.7? 如何在Python 2.7中正确使用这样的功能?

If anybody needs me to explain the Numpy/Matplotlib mechanisms used here, I will do my best to explain how they work to bring about the graphs. 如果有人需要我解释这里使用的Numpy / Matplotlib机制,我将尽力解释它们如何产生图形。

For the syntax error, check the line or two before . 对于语法错误,请在之前检查一两行。 t1 is a perfectly valid variable name (the rules are similar to various other languages: variable names can contain letters, numbers and _ , but can't start with a number) - but if you've forgotten a close bracket or similar above, then the interpreter will get confused and tell you you can't have an assignment statement there (newlines don't count as 'end of statement' if they're inside () , {} or [] , or if the last character before the newline is \\ ). t1是一个完全有效的变量名(规则类似于其他各种语言:变量名可以包含字母,数字和_ ,但不能以数字开头)-但是,如果您忘记了上面的右括号或类似的内容,则解释器会感到困惑,并告诉您那里没有赋值语句(如果换行符位于(){}[] ,或者如果之前的最后一个字符不包含在换行符中,则不会被视为“语句的结尾”换行符是\\ )。

Defining a function does give you a variable in that namespace with that name - you don't need to define f separately. 定义一个函数确实会为您提供该名称空间中具有该名称的变量-您无需单独定义f It is difficult to tell without seeing your exact code, but what has likely happened is either you have defined it in a different scope (so the name isn't visible), or you have renamed the function to something other than f . 看不到确切的代码很难分辨,但是可能发生的情况是您在另一个范围内定义了它(因此名称不可见),或者您已将函数重命名为f以外的名称。

Try typing everything again, in a new window. 尝试在新窗口中再次键入所有内容。 'f' isn't supposed to be defined separately. 'f'不应单独定义。

That error of invalid syntax seams to appear because the missing of a new line, and an unexpected indentation. 由于缺少新行和意外缩进,导致出现无效语法错误。

In the IDLE, after defining the function f(t) you have to press again <return> to finish the declaration of f(t) - putting an empty line after the declaration of f(t). 在IDLE中,在定义函数f(t)之后,您必须再次按<return>键以完成f(t)的声明-在f(t)的声明之后放置一个空行。 If you don't add that additional new line in the IDLE, you would get a syntax error because the unexpected indentation, and the definition of f(t) wouldn't be finished, so you also get the next error that says 'f' is not defined. 如果您未在IDLE中添加该新行,则会出现语法错误,因为意外的缩进和f(t)的定义将不会完成,因此您还会收到下一个错误,提示“ f ' 没有定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM