English 中文(简体)
页: 1 无 物体类型没有属性错误
原标题:Pythons s AttributeError: NoneType object has no attribute errors
  • 时间:2012-01-14 01:38:28
  •  标签:
  • python

I know it s been here a lot of times but I didn t find the right answer for my case.
First: I m making an easy database system (for myself) that will run with no hashes etc. (for now at least). Now I got stuck.

import sys
import os

filename = ""
database = ""
path = ""
table = ""

class Nollty:
    returns = 0
    errors = 0

    def __init__(self, filename, database):
        self.filename = filename
        self.database = database
        self.path = self.filename + "databases/" + self.database
        openfile = open(self.path + "/db_required", "r")
        if not openfile.errors:
            self.returns = 1
        if not os.path.exists(self.path + "/db_required"):
            self.returns = 0
        openfile.close();

    def select(self, table):
        errors = 0
        self.table = table
        openfile = open(self.path + "/" + self.table, "r")
        if not openfile.errors:
            errors = 1
        if not os.path.exists(self.path + "/" + self.table):
            errors = 0
        openfile.close();


nollty = Nollty("", "test")
if nollty.returns == 1:
    print "Successfully connected to the database!"

query = nollty.select("aaa_auto")
if query.errors == 0:
    print "Successfully chosen the table!"

错误产出是:

Traceback (most recent call last):
File "/home/spotrudloff/Python/Nollty/nollty.py", line 40, in <module>
if query.errors == 0:
AttributeError:  NoneType  object has no attribute  errors 

问题很可能是Im PHP方案管理员,今天,我几小时学会了Thury(因此我的想法仍然是“Py”。)

Thanks for all the responses.

最佳回答

<>编码>选择(>t 退还明确价值,因此具有<代码>NoneType的回报价值。 修改您的代码,以便select(>>,视您的代码是否成功或失败而定,将回复到1或0。

问题回答

你使用<代码>returns和errors,因为类别变量似乎并不好。 只有one例例例例列出这些变量,无论贵国创设多少次Nollty。 相反的:

def __init__(self, filename, database):
    self.returns = 0
    self.errors = 0
    # rest of __init__

其次,使用<代码>returns表示收益价值似乎也不好。 在沙尔,人们通常会提出但<>除外>,以表明施工者存在问题。 这样,打电话者就只能忽略一个问题,而不必去核对<>returns。

同样,使用<代码>电子表示()的例外情况来表示与参数有关的问题。 我的建议是eliminate both returns and err

您也不再从<条码>电子数据中收回任何价值,因此,请在<条码>上填写>。 结尾处为<条码>None(>,None,具有特殊价值。 你们要么希望从<条码>选择(<>条码/代码>)中收回一些有用的东西,要么不把结果分配到任何事情,如果它不回去有益价值的话。

它喜欢选择()不会恢复价值,因此,它拖欠了无Type(null)。 或者(你可能打算做些什么),将问题改变为损失。

选择方法中的错误变量是当地变量。 你们需要明确确定类别错误的变量。 另外,正如其他人所建议的,你的选择方法没有回报说明。 从你的法典来看,我们喜欢你试图获取不公的错误,但你正在对它进行质的变式检查,这种变式必然是无的。

你有两个选择,取决于您的目标。 重复从选择或自定的错误。 错误=在选择方法返回之前的错误。 或者,如果你愿意的话,情况如下。 通常,如果选择失败和真实,如果成功的话,你可能回落法尔,但并不需要。

class Nollty:
    ...
    def select(self,table):
        ...
        self.errors = errors
        return errors

errs = nollty.select("aaa_auto")
if nollty.errors == 0:
    print "Successfully chosen the table!"

##functionally equivalent
#if errs==0:
#    print "Successfully chosen the table!"




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签