看到如下的代码,不知道这两个类是什么关系?
class Connetiton(object):
def channel(self,on_open_callback,channel_number=None):
self._channels[channel_number]=self._create_channel(channel_number,on_open_callback)
def _create_channel(self,channel_number,on_open_callback):
return channel.Channel(self,channel_number,on_open_callback)
class Channel(object):
def __init__(self,connection,channel_number,on_open_callback=None):
self.connection=connection
在connection 类里会调用到Channel的类去channel
那么那个connection 和channel的关系是什么样?
这个是实例作为参数吗?
伊谢尔伦2017-04-17 17:43:42
These two classes have nothing to do with each other (if you have to say that they do, you can only say that they both inherit from object, but this is like telling you that they are classes in python, which is meaningless).
Python is case-sensitive. Connectiton and connection are two different things. Even if they are not case-sensitive, your expression is problematic. One is the name of the class, and the other is the name of the function parameter, which cannot be confused.
Even if the instance is used as a parameter, it should be var_name = Channel(Connetiton(var1,vqr2……),var2……)
Any python tutorial will have corresponding answers to questions about this basic concept. There is no need to ask additional questions in the community (so someone will deduct points for you...).
巴扎黑2017-04-17 17:43:42
It’s mentioned in great detail upstairs, so I’ll add a few more points here.
In your Channel
class, the connection
parameter of __init__()
is completely different from the Connection
class above. , insisting on contacting the relationship between the two of them: Channel
类中,__init__()
的connection
参数,和上面Connection
类,是完全两码事,硬要联系它们两个之间的关系:
1.它们名字一样(除了大小写)
2.它们都是对象
万物皆对象这个是题外话了,这里不谈。
所以在这里,它们两个是完全独立的两个东西。
你理解的“在channel
的init
函数中需要传入一个connection
的实例的参数”,不完全正确。
因为不知道你代码的需求,所以单从这两个类来看,Channel
类中的__init__()
方法有一个名叫connection
的参数,它看起来确实“有可能”需要传入Connection
rrreee
Everything is an object. This is a digression and will not be discussed here.
So here, they are two completely independent things.
init
function of channel
you need to pass in the parameters of an instance of connection
" is not entirely correct. __init__()
method in the Channel
class has a method named For the parameters of connection
, it seems that it is indeed "possible" that an instance of the Connection
class needs to be passed in. Developers will not randomly choose names when writing code, it is all for convenience. development and maintenance. But the 🎜real situation🎜 still depends on your 🎜code requirements and overall logic🎜, so this is just "possible". 🎜