我是Python 2.7的新手,已经从图书馆借用python书籍进行学习,这些书籍可能过于先进,但它们都是他们拥有的。我试图掰开代码想想Compexity由O'Reilly,大约字典辞典如下:了解括号在Python字典中的字典
class Graph(dict):
"""A Graph is a dictionary of dictionaries. The outer
dictionary maps from a vertex to an inner dictionary.
The inner dictionary maps from other vertices to edges.
For vertices a and b, graph[a][b] maps
to the edge that connects a->b, if it exists."""
def __init__(self, vs=[], es=[]):
"""Creates a new graph.
vs: list of vertices;
es: list of edges.
"""
for v in vs:
self.add_vertex(v)
for e in es:
self.add_edge(e)
def add_vertex(self, v):
"""Add a vertex to the graph."""
self[v] = {}
def add_edge(self, e):
"""Adds and edge to the graph by adding an entry in both directions.
If there is already an edge connecting these Vertices, the
new edge replaces it.
"""
v, w = e
self[v][w] = e
self[w][v] = e
我无法理解什么是括号里面的代码做,如def __init__(self, vs=[], es=[])
。我得到它分配属性,“vs”和“es”是列表,但为什么不在函数内部声明?为什么“自我”在那里? def add_vertex(self, v)
和def add_edge(self, e)
也是如此。
无法使用以下函数在函数内声明列表?
vs = {}
你应该阅读[学习Python](http://shop.oreilly.com/product/0636920028154.do),并学习关于Python的基础知识,特别是参数(第18章 - 涉及你的'vs' /'es问题)和课堂/面向对象(第六部分 - 涉及到你的“自我”问题)。 –