1

I have a utility where I have dozens of function and many classes that read an excel file. I want to ensure that python only goes thru the operation of opening the file once, and that one instance would be reused anywhere in the system. Coming from the java world, which is my primary language, this would be a simple singleton. I found this post

Is there a simple, elegant way to define singletons?

Where there is a singleton decoration that i really like for no other reason than its familiar.

Is this design pattern the most appropriate way to pull off this kind of functionality or is there a more pythonish pattern i should use.

thanks.

Community
  • 1
  • 1
scphantm
  • 4,293
  • 8
  • 43
  • 77
  • I think you're confusing "opening the file once" with Singleton. These two are not related. You can have a Singleton instance open the same file multiple times... – Nir Alfasi Apr 24 '15 at 16:38
  • Write a function to read the file (thus ensuring everywhere formats the data consistently) and then memoize the return value – kdopen Apr 24 '15 at 16:48
  • what mechanism do i use to pass that memorized return around? in my normal world, i would memorize it in the form of a singleton that lives in the JVM and all things access. Its not thread safe mind you, but its appropriate for my use case – scphantm Apr 24 '15 at 17:30

0 Answers0