python 类和实例
高洛峰
高洛峰 2017-04-17 17:41:42
0
2
382

看到如下的代码,不知道这两个类是什么关系?

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的关系是什么样?
这个是实例作为参数吗?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (2)
伊谢尔伦
  • 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 bevar_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...).

    巴扎黑

    It’s mentioned in great detail upstairs, so I’ll add a few more points here.

    In yourChannelclass, theconnectionparameter of__init__()is completely different from theConnectionclass above. , insisting on contacting the relationship between the two of them:Channel类中,__init__()connection参数,和上面Connection类,是完全两码事,硬要联系它们两个之间的关系:

    1.它们名字一样(除了大小写) 2.它们都是对象

    万物皆对象这个是题外话了,这里不谈。

    所以在这里,它们两个是完全独立的两个东西。

    你理解的“在channelinit函数中需要传入一个connection的实例的参数”,不完全正确。
    因为不知道你代码的需求,所以单从这两个类来看,Channel类中的__init__()方法有一个名叫connection的参数,它看起来确实“有可能”需要传入ConnectionrrreeeEverything is an object. This is a digression and will not be discussed here.So here, they are two completely independent things.

    Your understanding of "In the initfunction of channelyou need to pass in the parameters of an instance of connection" is not entirely correct.
    Because I don’t know the requirements of your code, so from the perspective of these two classes, the __init__()method in the Channelclass has a method named For the parameters of connection, it seems that it is indeed "possible" that an instance of the Connectionclass 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".
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!