0

I have two files. SysDump.py which does from libApi import _SysDump and in the other file libApi.py I have many classes and one of them is

class _SysDump():
    import cPickle as _cPickle
    import math as _math
    from zipfile import ZipFile as _ZipFile
    import re as _re 

the problem is the import in SysDump not only sees the _SysDump in libApi.py but other classes too!! How can I prevent it from seeing inside the other classes?

Python 2.6. Yes I do have a __init__.py. Can it in any way help bring the privacy I am hoping for?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Santhosh Kumar
  • 381
  • 1
  • 5
  • 13
  • Doesn't "pulling up" the imports out of the class do the trick? – fge Jan 05 '13 at 19:18
  • 1
    What do you mean, it "sees" the other classes? You've only imported `_SysDump`, that will be the only name defined in SysDump.py – Ned Batchelder Jan 05 '13 at 19:19
  • @NedBatchelder I believe the OP means that importing this class also makes `_cPickle` et al available – fge Jan 05 '13 at 19:20
  • @fge it wouldn't do that, so I'll be interested to hear what the OP meant. – Ned Batchelder Jan 05 '13 at 19:21
  • @NedBatchelder it'll be available via _SysDump (as _SysDump._cPickle). – James Aylett Jan 05 '13 at 19:22
  • in libApi.py other than the class _SysDump() I have class abcd(): with a bunch of imports, class xyz(): with more imports. Like that I have many classes each with its own set of imports. I want to see only the imports inside class _SysDump() which is not happening – Santhosh Kumar Jan 05 '13 at 19:30
  • @SanthoshKumar: you still have not defined what you mean by "see". Only one name is defined in your importing file. What is the problem? – Ned Batchelder Jan 05 '13 at 19:36
  • Sorry, What I mean is the imports in other classes get defined to me which I dont want. What happens is when the imports from the non wanted class get defined the behavior of my program changes. So I want only the imports concerned to SyDump none else – Santhosh Kumar Jan 05 '13 at 19:42
  • @SanthoshKumar: I'm sorry, I don't yet understand how those imports affect your program. Perhaps you could provide a more complete sample? In any case, importing a module fully executes the imported Python file, there's no way for it to execute only part of it. – Ned Batchelder Jan 05 '13 at 19:43
  • Sorry let me try,kindly bear with me. Let me state my problem in a different way. Assume I have two classes defined in libApi.py one is the class _SysDump(): and the other is class abcd():. Both of them have a set of imports. In SysDump.py I do from libApi import _SysDump and then run my code and it fails. I comment out class abcd and my program works. That means class abcd(): has adverse affect on my program. However this libApi is accessed by other programs and for them the imports under class abcd(): is relevant and not my class _SysDump(). That is the problem. – Santhosh Kumar Jan 05 '13 at 19:54
  • @SanthoshKumar: Ok, then you need to find out what is wrong with class abcd(). You haven't mentioned what the failure is, but this can definitely be solved. Perhaps ask in the #python IRC channel on freenode? Or ask a more complete question here, with the code. – Ned Batchelder Jan 05 '13 at 20:03
  • possible duplicate of [Why are Python's 'private' methods not actually private?](http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private) – Bibhas Debnath Feb 13 '14 at 23:39

1 Answers1

2

Python has no notion of privacy. If you are trying to hide information in one module from another module which imports it, you cannot. That is a concept from other programming languages which has no parallel in Python.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • To be clear: Of course, Python programmers do have that notion and there's a convention (underscore prefix) for indicating that intent. There's just no such thing in the language. –  Jan 05 '13 at 19:23
  • I agree but when we do specifically from libApi import _SysDump should'nt it mean From libApi.py go get me the imports inside the class _SysDump only? – Santhosh Kumar Jan 05 '13 at 19:32
  • 2
    @SanthoshKumar: when you import a module, the entire module.py file is executed. Then the names you want to import are defined in your file. There is no way to execute only part of a Python file. – Ned Batchelder Jan 05 '13 at 19:35