python中意外缩进是什么意思_Python类方法定义:“意外缩进”(Python Class method definition : “unexpected indent”)...

Python类方法定义:“意外缩进”(Python Class method definition : “unexpected indent”)

我开始使用Django和Python,所以很自然地我正在进行民意调查项目教程。 我在Windows 7下使用Python 2.7.9和Django 1.3.7

我有这段代码(括号下的行号):

(3) class Poll(models.Model):

(4) question = models.CharField(max_length=200)

(5) pub_date = models.DateTimeField('date published')

(6) def __unicode__(self):

(7) return self.question

当尝试运行“manage.py shell”时,我收到以下错误:

文件“C:\ Users ... \ mysite \ polls \ models.py”,第6行

def unicode (个体经营)

^

IndentationError:意外缩进

我究竟做错了什么 ?

I am getting started with Django and Python so naturally I'm doing the polls project tutorial. I am working under Windows 7 with Python 2.7.9 and Django 1.3.7

I have this piece of code (with line numbers under brackets):

(3) class Poll(models.Model):

(4) question = models.CharField(max_length=200)

(5) pub_date = models.DateTimeField('date published')

(6) def __unicode__(self):

(7) return self.question

and when trying to run "manage.py shell" I get the following error :

File "C:\Users...\mysite\polls\models.py", line 6

def unicode(self)

^

IndentationError: unexpected indent

What am I doing wrong ?

原文:https://stackoverflow.com/questions/27460631

2020-05-10 06:05

满意答案

您的class标题与其内容的缩进相同,您需要优化缩进( 以pythonic方式使用4个空格进行缩进 ):

class Poll(models.Model):

question = models.CharField(max_length=200)

pub_date = models.DateTimeField('date published')

def __unicode__(self):

return self.question

Your class header is in same indentation with its content , you need to refine the indentation (as a pythonic way use 4 space for indentation ):

class Poll(models.Model):

question = models.CharField(max_length=200)

pub_date = models.DateTimeField('date published')

def __unicode__(self):

return self.question

2014-12-13

相关问答

在你的hexdump中,在第一行代码之前有两个空格。 这意味着你的初始缩进是两个空格,而python根本不需要空格(或制表符)。 字节0x9e和0x9f是空格,字节0xa0是dict = {}的'd'。 如果选项'hlsearch'已启用,则可以使用/^\s*查看起始空格。 In your hexdump there are two spaces before the first line of code. This means your initial indent was two spaces...

该解决方案由Ashwini Chaudhary发布,其工作原理如下: 使用-tt:python -tt script_name.py运行脚本并删除该return语句。 return语句只能在函数内部使用 This solution, posted by Ashwini Chaudhary worked: Run your script using -tt: python -tt script_name.py and remove that return statement. return stat...

只有在使用-wthread调用-wthread时才会发生这种情况,我刚刚提交了一个关于此的错误 。 This happens only when ipython is called with -wthread, I've just filed a bug about this.

在Python中,缩进很重要,这个错误说明你的缩进有问题。 在这种情况下,if语句不需要缩进。 所以正确的代码就是 print "Gamoicanit chafiqrebuli ricxvi [1,250]"

print "Sirtule - Sheudzlebeli."

print "Mcdelobebis raodenoba - 7"

tryh1 = raw_input("< ")

if (int(tryh1) > int(rand4)):

print "Mititebuli ric...

你在混合标签和空格。 不要这样做,它会产生不一致的缩进问题。 通过选项卡检查器运行脚本: python -tt script.py

并修复任何和所有标签(用空格替换),然后将您的编辑器配置为仅使用空格。 对于Notepad ++,请参阅: 将制表符转换为Notepad ++中的空格 如何配置记事本++使用空格而不是制表符? You are mixing tabs and spaces. Don't do this, it creates inconsistent indentation prob...

看起来你在混合标签和空格。 选择查看View->Indentation ,并确保选中Indent Using Spaces ,然后点击Convert Indentation to Spaces ,你应该全部设置。 It looks like you are mixing tabs and spaces. Select View->Indentation and make sure that Indent Using Spaces is checked, then click Convert Ind...

命令跳转到第一个标记匹配,但它也需要[count]跳转到另一个。 或者,您可以使用g命令(如:tjump Ex命令)将列出所有匹配项并查询您要跳转到的位置(当存在多个匹配项时)。 The command jumps to the first tag match, but it also takes a [count] to jump to another one. Alternatively, you can use the g command, whic...

假设复制到SE时这不是复制和粘贴错误,您需要更改第一次返回的缩进: from fast_rcnn.config import cfg

from nms.cpu_nms import cpu_nms

def nms(dets, thresh, force_cpu=False):

"""Dispatch to either CPU or GPU NMS implementations."""

if (dets.shape[0]) == 0:

# Note the c...

services = ['Service1']与for循环不一致。 services = ['Service1']

for service in services: <-- unexpected indentation

只需按如下方式排列代码即可。 services = ['Service1'] # need to line up with loop

for service in services:

try:

print service + '\t',

...

您的class标题与其内容的缩进相同,您需要优化缩进( 以pythonic方式使用4个空格进行缩进 ): class Poll(models.Model):

question = models.CharField(max_length=200)

pub_date = models.DateTimeField('date published')

def __unicode__(self):

return self.question

Your class he...

相关文章

3.2 工厂方法模式与IoC/DI,IoC——Inversion of Control 控制反转,

...

又重新看了下hbase的操作,以前虽说是运行过对Hbase的操作,比如直接的建表,导入数据,或者是使用

...

Java类用于描述一类事物的共性,该类事物有什么属性,没有什么属性,至于这个属性的值是什么,则是由这个

...

3.1 认识工厂方法模式(1)模式的功能 工厂方法的主要功能是让父类在不知道具体实现的情况下,完成自

...

3.3 平行的类层次结构(1)什么是平行的类层次结构呢?简单点说,假如有两个类层次结构,其中一个类层

...

2.1 工厂方法模式来解决 用来解决上述问题的一个合理的解决方案就是工厂方法模式。那么什么是工厂方法

...

在python中, 去除了i > 0周围的括号,去除了每个语句句尾的分号,还去除了表示块的花括号。多出

...

.net的动态编译功能很好,但是有个问题,动态编译的代码,每次执行后,都会产生一个新的assembly

...

1.1 导出数据的应用框架,考虑这样一个实际应用:实现一个导出数据的应用框架,来让客户选择数据的导出

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值