Python 点连接的变量
我对Python非常陌生,非常喜欢它。 然而,在阅读一些代码时,我很难理解为什么一些变量是由点连接的 下面是从同一个文件中提取的一些示例Python 点连接的变量,python,variables,properties,Python,Variables,Properties,我对Python非常陌生,非常喜欢它。 然而,在阅读一些代码时,我很难理解为什么一些变量是由点连接的 下面是从同一个文件中提取的一些示例 class Room(object): def __init__(self, name, description): self.name = name self.description = description self.paths = {} def go(self, direction):
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.paths = {}
def go(self, direction):
return self.paths.get(direction, None)
def add_paths(self, paths):
self.paths.update(paths)
及
我不明白的是那些带有点的东西,所以self.paths.update(paths)
或self.description
等等
我不知道为什么要使用它,哪些变量必须连接,什么时候我必须使用它。它们是“属性”。看
简言之,我假设您熟悉字典:
dct = {'foo': 'bar'}
print dct['foo']
对于类,属性的行为非常相似:
class Foo(object):
bar = None
f = Foo()
f.bar = 'baz'
print f.bar
(事实上,这通常是对类实例的字典查找)。当然,一旦您理解了这一点,您就会意识到这不仅仅适用于类——它也适用于模块和包。简而言之,
是从某个对象获取属性(或根据上下文设置属性)的操作符。它们是“属性”。看
简言之,我假设您熟悉字典:
dct = {'foo': 'bar'}
print dct['foo']
对于类,属性的行为非常相似:
class Foo(object):
bar = None
f = Foo()
f.bar = 'baz'
print f.bar
(事实上,这通常是对类实例的字典查找)。当然,一旦您理解了这一点,您就会意识到这不仅仅适用于类——它也适用于模块和包。简而言之,
是从某个对象获取属性(或根据上下文设置属性)的操作符。阅读Python中的OOP和类。阅读Python中的OOP和类。