开发者

Python: Is passing a function more information a bad thing?

开发者 https://www.devze.com 2023-03-10 00:16 出处:网络
I have two or more threads(created by subclassing threading.Thread) running in parallel and they often communicate with each other. They communicate by calling each others methods. As I have been d开发

I have two or more threads(created by subclassing threading.Thread) running in parallel and they often communicate with each other. They communicate by calling each others methods. As I have been d开发者_如何学编程oing it, I have each thread pass itself as an argument, so that all of that classes attributes are available right away.

This is probably the lazy way of doing is as I could figure which attributes a method needs to know, and only pass those. So my question is this, is passing a whole class instance to a method less efficient than just the relevant data?


It's not less efficient, but it can increase coupling -- that is, it might lead you to make your function more dependent on the details of the overall structure. This makes code more brittle -- you might have to make more changes throughout the code to account for a change in the structure.


Python passes references to objects. Doesn't matter if your object is a string or a whole class instance full of attributes.

BTW, as said on other answer, this design choice isn't the best one.

0

精彩评论

暂无评论...
验证码 换一张
取 消