Pass Variable On Import

后端 未结 8 1395
轻奢々
轻奢々 2020-12-16 09:37

Let\'s say you have some time-consuming work to do when a module/class is first imported. This functionality is dependent on a passed in variable. It only needs to be done

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 10:16

    No you're not stuck with a module level function, it's just probably the best option. You could also use the builtin staticmethod or classmethod decorators to make it a method on someSclass that can be called before it is instantiated.

    This would make sense only if everything other than someClass was usable without the initialization and I still think a module level function is better.

提交回复
热议问题