Python 类的基本知识
在DGL的source code中发现
class DistGraphServer(KVServer):
而KVServer亦是一个类
class KVServer(object):
"""KVServer is a lightweight key-value store service for DGL distributed training.
In practice, developers can use KVServer to hold large-scale graph features or
graph embeddings across machines in a distributed setting. KVServer depends on DGL rpc
infrastructure thats support backup servers, which means we can lunach many KVServers
on the same machine for load-balancing.
DO NOT use KVServer in mult-threads because this behavior is not defined. For now, KVServer
can only support CPU-to-CPU communication. We may support GPU-communication in the future.
Parameters
----------
server_id : int
ID of current server (starts from 0).
ip_config : str
Path of IP configuration file.
num_servers : int
Server count on each machine.
num_clients : int
Total number of KVClients that will be connected to the KVServer.
"""
def __init__(self, server_id, ip_config, num_servers, num_clients):
而在某段代码中调用第一个类的方法为
serv = DistGraphServer(int(os.environ.get('DGL_SERVER_ID')),
os.environ.get('DGL_IP_CONFIG'),
int(os.environ.get('DGL_NUM_SERVER')),
int(os.environ.get('DGL_NUM_CLIENT')),
os.environ.get('DGL_CONF_PATH'),
graph_format=formats)
参考--------->python面向对象中类的继承,就是一个类的继承问题
同时blog中还介绍了一些如
–init–
–del–
–str–
等的python类通用功能,这些都可以重写。
关于单下划线、双下划线、双尾下划线的说明
- __ foo __: 定义的是特殊方法,一般是系统定义名字 ,类似 __ init __() 之类的。又叫魔术方法,常用的可参考python特殊类,这里面的—iter—比较重要,可用from collections import Iterable然后isinstance(classname, Iterable)来判断一个类是否是否为可迭代类
- _foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行访问,不能用于 from module import *
- __foo: 双下划线的表示的是私有类型(private)的变量, 只能是允许这个类本身进行访问了。