Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-无法访问类中的变量_Python - Fatal编程技术网

Python-无法访问类中的变量

Python-无法访问类中的变量,python,Python,我有一个基本的python问题。我正在处理一个类foo,我使用\uuu init\uuu():对一个值执行一些操作: class foo(): def __init__(self,bar=None): self.bar=bar if bar is None: isSet=False else: isSet=True print isSet 当我执行我得到的代码时:name错误:未定义名称“isSet” 如何访问isSet?我做错

我有一个基本的python问题。我正在处理一个类
foo
,我使用
\uuu init\uuu():
对一个值执行一些操作:

class foo():
  def __init__(self,bar=None):
    self.bar=bar
    if bar is None: 
       isSet=False  
    else:
       isSet=True
  print isSet
当我执行我得到的代码时:
name错误:未定义名称“isSet”

如何访问isSet?我做错了什么

问候,,
martin

缩进错误,应改为此缩进,否则将退出函数

class foo():
  def __init__(self,bar=None):
    self.bar=bar

    if bar is None: 
       isSet=False  
    else:
       isSet=True

    print isSet

缩进错误,应改为此,否则将退出函数

class foo():
  def __init__(self,bar=None):
    self.bar=bar

    if bar is None: 
       isSet=False  
    else:
       isSet=True

    print isSet

这取决于你想做什么。您可能希望
isSet
成为
foo
的成员:

class foo():
  def __init__(self,bar=None):
    self.bar=bar
    self.isSet=True
    if bar is None: 
       self.isSet=False  

f = foo()
print f.isSet

这取决于你想做什么。您可能希望
isSet
成为
foo
的成员:

class foo():
  def __init__(self,bar=None):
    self.bar=bar
    self.isSet=True
    if bar is None: 
       self.isSet=False  

f = foo()
print f.isSet

最后一行的缩进使它在类的上下文中执行,而不是
\uuuu init\uuuu
。再缩进一次以使程序工作。

最后一行的缩进使其在类的上下文中执行,而不是在
\uuuuu init\uuuu
中执行。再缩进一次以使程序正常工作。

我会试试

class foo():
  def __init__(self,bar=None):
    self.bar = bar
    self.is_set = (bar is None)
然后在课堂上

...
def spam(self, eggs, ham):
    if self.is_set:
        print("Camelot!")
    else:
...
你读了吗?

我想试试

class foo():
  def __init__(self,bar=None):
    self.bar = bar
    self.is_set = (bar is None)
然后在课堂上

...
def spam(self, eggs, ham):
    if self.is_set:
        print("Camelot!")
    else:
...

你读过吗?

在你的代码中,
isSet
是一个类属性,而不是实例属性。因此,在类主体中,您需要在
print
语句中引用它之前对其进行定义。在类外部及其方法中,必须在其前面加上类名和点,在本例中为
Foo.
。请注意,
def\uuu init\uuu()
函数体中的代码在通过调用创建
Foo
类的实例之前不会执行

class Foo(object):
    isSet = None

    def __init__(self, bar=None):
        self.bar = bar

        if bar is None:
           Foo.isSet = False
        else:
           Foo.isSet = True

    print isSet

f = Foo()
print Foo.isSet
输出:

None
False

在代码中,
isSet
是类属性,而不是实例属性。因此,在类主体中,您需要在
print
语句中引用它之前对其进行定义。在类外部及其方法中,必须在其前面加上类名和点,在本例中为
Foo.
。请注意,
def\uuu init\uuu()
函数体中的代码在通过调用创建
Foo
类的实例之前不会执行

class Foo(object):
    isSet = None

    def __init__(self, bar=None):
        self.bar = bar

        if bar is None:
           Foo.isSet = False
        else:
           Foo.isSet = True

    print isSet

f = Foo()
print Foo.isSet
输出:

None
False

这正是我想要的,我想在类的上下文中执行它,而不是在
init
@Martin“在类的上下文中执行”意味着什么?如果它意味着在成员方法内部,请参阅答案,如果它意味着在类声明中,您不能访问在
\uuuu init\uuuu
中创建的任何内容,因为
\uuuu init\uuuu
从未被调用过。这正是我想要的,我想在类的上下文中执行它,而不是在
init
@Martin what做什么“在类的上下文中执行”“什么意思?如果它意味着在成员方法内部,请参阅答案,如果它意味着在类声明中,您不能访问在
\uuuu init\uuuu
中创建的任何内容,因为
\uuuu init\uuuuu
从未被调用过。是的,我想在类的上下文中执行它。所以压痕是正确的。我想从类中的代码访问isSet,而不是在def _init _()@Martin:那就没有意义了,因为
\u_init _;
只在创建实例时运行,而不是在创建类时运行。你到底想做什么?我想做以下事情:a=foo(bar=0x03)。然后检查在创建对象时是否设置了bar(imho这应该使用_uinit__;()完成),然后我想在类中使用该
bar
。也就是说,我想根据变量
bar
的值执行不同的操作。我希望你能理解。谢谢你的帮助help@Martin:仍然看不出为什么在类范围中需要它。那里的代码是在创建类型时执行的,远远早于创建任何实例,因此您无法访问任何将由\uuuuu init\uuuuu(或任何其他方法)产生的结果。是的,我想在类的上下文中执行它。所以压痕是正确的。我想从类中的代码访问isSet,而不是在def _init _()@Martin:那就没有意义了,因为
\u_init _;
只在创建实例时运行,而不是在创建类时运行。你到底想做什么?我想做以下事情:a=foo(bar=0x03)。然后检查在创建对象时是否设置了bar(imho这应该使用_uinit__;()完成),然后我想在类中使用该
bar
。也就是说,我想根据变量
bar
的值执行不同的操作。我希望你能理解。谢谢你的帮助help@Martin:仍然看不出为什么在类范围中需要它。在创建类型时,在创建任何实例之前很久,就会执行其中的代码,因此您无法访问任何将由\uuuuu init\uuuuuu(或任何其他方法)产生的结果。您的类名应为Foo,因为类名应根据使用大写DWORDS约定,并且其成员也应为is\u set,因为成员使用带有下划线的小写字母,所以您的类名应该是
Foo
,因为根据惯例,类名应该使用大写单词,他的成员也应该是is_set,因为成员使用带有下划线的小写字母。