讨厌的行为 [英] annoying behavior

查看:69
本文介绍了讨厌的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#这是我遇到的问题:


class foo:

def __init __(自我,主持人):

self.f()

self.r = True


def f(self):

if self.r:

#<做点什么>

通过

else:

#<做其他事情>

通过


f = foo(" 1234")

#是输出:


#Traceback(最近一次调用最后一次):

#File" G:\ MyProjects\Python\Little\inconv.py",第16行,in ?

#f = foo(" 1234")

#File" G:\ MyProjects\Python \ Little \ inconv.py",line 5,in __init__

#self.f()

#File" G:\ MyProjects\Python\Little\inconv.py",第9行,在f

#if self.r:

#AttributeError:foo实例没有属性''r''


#我明白为什么会这样,但是,说实话,

#这个功能很烦人。

#有没有计划放宽这个限制?

#在3.0中: )?

解决方案

Elbert Lev写道:

#这是我遇到的问题:
[code在它绑定到任何东西之前使用一个名称] #here是输出:
#AttributeError:foo实例没有属性''r''

#我明白为什么这样做发生了,但是,说实话,
#这个功能非常烦人。
#有没有计划放宽这个限制?
#在3.0中:)?




呃,有什么特色?您的代码中存在错误。什么

你表现出的* no *不同于这样做和

期待错误以外的东西:

print x
x = 5




请详细说明详细说明为什么你认为这个

不应该给出一个错误,你认为它应该给你什么输出



-Peter


Elbert Lev写道:

#这是我遇到的问题:

类foo:
def __init __(self,host):
self.f()
self.r = True
[在存在之前使用属性剪切代码] #here是输出:#AttributeError:foo实例没有属性''r''

#我明白为什么会这样,但是,说实话,
#这个功能非常烦人。


此功能很有用! :)

#有没有计划放宽这个限制?
#3.0:)?




天哪,我确定希望不是! (我也对此表示怀疑)你有什么行为?
介意?任何与当前行为不同的东西都会令人困惑/误导

并且会隐藏你遇到的错误。


-Dave


Elbert Lev写道:

#这是我遇到的问题:

class foo:
def __init__ (自我,主持人):
self.f()
self.r = True

def f(self):
if self.r:
#<做某事>
传递
否则:
#<做其他事情>
传递

f = foo(" 1234" )

#here是输出:

#Traceback(最近一次调用最后一次):
#FileG:\ MyProjects \ Python \ Little\inconv.py",第16行,在?
#f = foo(" 1234")
#FileG:\ MyProjects \ Python \ Little \ inconv。 py",第5行,在__init__
#self.f()
#FileG:\ MyProjects \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #>#我明白为什么会这样,但是,说实话,这个功能非常烦人。
#有没有计划放宽这个限制?
#在3.0中: )?



放宽限制?为什么这个行为与它有什么不同?

是?我将上述代码等同于:

f = {}
f ['' a'']
回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

KeyError:' 'a''f [''a''] = 1



Python怎么知道我要指定f [''a'']什么之前

被分配之前?这就像期待这样:

f = {}
f [''a'']
#我正计划设置f [''a'''一点点的东西 -
也许Python解释器可以弄明白我要做什么....

1 f [''a''] = 1




来生成我刚输入的结果。你正在调用方法f()

在将属性r设置为任何内容之前,所以当你调用f()时,

属性r没有设置,所以你得到一个例外。你有什么限制?b $ b想放松一下?你想要Python解释器提前读取吗?

弄清楚你是否打算设置一个变量然后使用你计划在将来设置的一些

变量?如果是这样,如果你设置它然后重新设置它是什么

?它应该选择哪一个?


Jeremy


# here is the problem I ran into:

class foo:
def __init__(self, host):
self.f()
self.r = True

def f(self):
if self.r:
#<do something>
pass
else:
#<do something else>
pass

f = foo("1234")

#here is the output:

#Traceback (most recent call last):
# File "G:\MyProjects\Python\Little\inconv.py", line 16, in ?
# f = foo("1234")
# File "G:\MyProjects\Python\Little\inconv.py", line 5, in __init__
# self.f()
# File "G:\MyProjects\Python\Little\inconv.py", line 9, in f
# if self.r:
#AttributeError: foo instance has no attribute ''r''

# I understand why does this happen, but, to tell the truth,
# this feature is very annoying.
# Are there any plans to relax this restriction?
# In 3.0 :)?

解决方案

Elbert Lev wrote:

# here is the problem I ran into: [code that uses a name before it''s bound to anything] #here is the output:
#AttributeError: foo instance has no attribute ''r''

# I understand why does this happen, but, to tell the truth,
# this feature is very annoying.
# Are there any plans to relax this restriction?
# In 3.0 :)?



Uh, what feature? You have a bug in your code. What
you showed is *no* different than doing this and
expecting something other than an error:

print x
x = 5



Please explain in more detail why you think this
shouldn''t be giving an error, and what output you
think it should have given you.

-Peter


Elbert Lev wrote:

# here is the problem I ran into:

class foo:
def __init__(self, host):
self.f()
self.r = True [snip code using attribute before it existed] #here is the output:
#AttributeError: foo instance has no attribute ''r''

# I understand why does this happen, but, to tell the truth,
# this feature is very annoying.
This "feature" is pretty useful! :)
# Are there any plans to relax this restriction?
# In 3.0 :)?



Goodness, I sure hope not! (and I doubt it too) What behavior do you have in
mind? Anything different than the current behavior would be confusing/misleading
and would hide bugs like the one you encountered.

-Dave


Elbert Lev wrote:

# here is the problem I ran into:

class foo:
def __init__(self, host):
self.f()
self.r = True

def f(self):
if self.r:
#<do something>
pass
else:
#<do something else>
pass

f = foo("1234")

#here is the output:

#Traceback (most recent call last):
# File "G:\MyProjects\Python\Little\inconv.py", line 16, in ?
# f = foo("1234")
# File "G:\MyProjects\Python\Little\inconv.py", line 5, in __init__
# self.f()
# File "G:\MyProjects\Python\Little\inconv.py", line 9, in f
# if self.r:
#AttributeError: foo instance has no attribute ''r''

# I understand why does this happen, but, to tell the truth,
# this feature is very annoying.
# Are there any plans to relax this restriction?
# In 3.0 :)?


Relax what restriction? Why should this behave any differently than it
is? I would kind of equate the above code to:

f = {}
f[''a''] Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: ''a'' f[''a''] = 1

How should Python know that I''m going to assign f[''a''] to anything
before it gets assigned? That''s like expecting this:
f = {}
f[''a'']
#I''m planning on setting f[''a''] to something in a little bit - maybe the Python interpreter can figure out what I''m going to do....
1 f[''a''] = 1



to produce the results that I just typed in. You''re calling method f()
before you are setting attribute r to anything, so when you call f(),
attribute r isn''t set, so you get an exception. What restriction do you
want to relax? Do you want the Python interpreter to read ahead and
figure out if you were intending to set a variable and then use some
variable that you were planning on setting in the future? If so, what
if you set it and then set it again? Which one should it choose?

Jeremy


这篇关于讨厌的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆