我正在尝试从http://www.python-course.eu/graphs_python.php了解以下类定义:
//Routes for all admin control panel
Route::get('/admin','Admin\LoginController@viewlogin');
Route::post('/admin/login','Admin\LoginController@checklogin');
Route::group(['middleware' => ['admins']], function ()
{
Route::get('/admin/dashboard','Admin\AdminController@dashboard');
Route::get('/admin/logout','Admin\LoginController@logout');
Route::resource('/admin/movies','Admin\MovieController');
Route::resource('/admin/states','Admin\StateController');
Route::resource('/admin/cities','Admin\CityController');
Route::resource('/admin/tax','Admin\TaxController');
Route::resource('/admin/smsgateway','Admin\SmsgatewayController');
Route::resource('/admin/smtpgateway','Admin\SmtpgatewayController');
Route::resource('/admin/paymentgateway','Admin\PaymentgatewayController');
Route::resource('/admin/news','Admin\NewsController');
Route::resource('/admin/promotion','Admin\PromotionController');
Route::resource('/admin/staff','Admin\StaffController');
Route::get('/admin/staff/resetpass/{id}','Admin\StaffController@resetpass');
});
类定义中""" A Python Class
A simple Python graph class, demonstrating the essential
facts and functionalities of graphs.
"""
class Graph(object):
def __init__(self, graph_dict={}):
""" initializes a graph object """
self.__graph_dict = graph_dict
def vertices(self):
""" returns the vertices of a graph """
return list(self.__graph_dict.keys())
def edges(self):
""" returns the edges of a graph """
return self.__generate_edges()
def add_vertex(self, vertex):
""" If the vertex "vertex" is not in
self.__graph_dict, a key "vertex" with an empty
list as a value is added to the dictionary.
Otherwise nothing has to be done.
"""
if vertex not in self.__graph_dict:
self.__graph_dict[vertex] = []
def add_edge(self, edge):
""" assumes that edge is of type set, tuple or list;
between two vertices can be multiple edges!
"""
edge = set(edge)
(vertex1, vertex2) = tuple(edge)
if vertex1 in self.__graph_dict:
self.__graph_dict[vertex1].append(vertex2)
else:
self.__graph_dict[vertex1] = [vertex2]
def __generate_edges(self):
""" A static method generating the edges of the
graph "graph". Edges are represented as sets
with one (a loop back to the vertex) or two
vertices
"""
edges = []
for vertex in self.__graph_dict:
for neighbour in self.__graph_dict[vertex]:
if {neighbour, vertex} not in edges:
edges.append({vertex, neighbour})
return edges
def __str__(self):
res = "vertices: "
for k in self.__graph_dict:
res += str(k) + " "
res += "\nedges: "
for edge in self.__generate_edges():
res += str(edge) + " "
return res
if __name__ == "__main__":
g = { "a" : ["d"],
"b" : ["c"],
"c" : ["b", "c", "d", "e"],
"d" : ["a", "c"],
"e" : ["c"],
"f" : []
}
graph = Graph(g)
print("Vertices of graph:")
print(graph.vertices())
print("Edges of graph:")
print(graph.edges())
print("Add vertex:")
graph.add_vertex("z")
print("Vertices of graph:")
print(graph.vertices())
print("Add an edge:")
graph.add_edge({"a","z"})
print("Vertices of graph:")
print(graph.vertices())
print("Edges of graph:")
print(graph.edges())
print('Adding an edge {"x","y"} with new vertices:')
graph.add_edge({"x","y"})
print("Vertices of graph:")
print(graph.vertices())
print("Edges of graph:")
print(graph.edges())
输入参数的重点是什么?如果我只是省略它并将定义作为(object)
开始,那么代码似乎也可以运行。