Linked Questions

1 vote
2 answers
4k views

How do you correctly use private functions in Python? [duplicate]

I'm new to Python and I'm facing some issues when it comes to private functions. I want to call two of them inside a public method, just for the sake of making the code look clear, but I cannot ...
Julian Ailan's user avatar
1 vote
2 answers
3k views

Is there a way to disable some function in python class so that it cannot be used except using it in inside its class? [duplicate]

for example i have myClassFile.py file with code as follow: class myClass: def first(self): return 'tea' def second(self): print(f'drink {self.first()}') then i have run.py ...
Hanzcerb's user avatar
1 vote
1 answer
2k views

How to allow python file to export specific functions that can be imported later? [duplicate]

Suppose I have 2 functions and one global variable in a python file. How do I allow to export only one function? g = 47 def A(): print('a') def B(): print('b') Suppose the above file is ...
Faizan's user avatar
  • 23
0 votes
1 answer
985 views

how to not expose methods in python [duplicate]

how can we not expose methods in Python and make them private as in Java? specifically, my scenario involves functions that the user should not be using.
Neil's user avatar
  • 499
1 vote
0 answers
173 views

How to create a non-callable method from class? [duplicate]

I have a class which has two methods(m_1 and m_2). The method m_1 makes a call to method m_2. Now i want to make sure that only m_1 can be called using a class insance and not m_2. How can i do that ? ...
cadip92's user avatar
  • 151
0 votes
0 answers
104 views

Function visible to the module classes but not outside [duplicate]

I have various class in a module that execute some operation. Since all of them require to connect to a remote server to execute these operation, I thought that is not convenient to have a ton of ...
user avatar
1788 votes
10 answers
724k views

What does __all__ mean in Python?

I see __all__ in __init__.py files. What does it do?
varikin's user avatar
  • 18.3k
855 votes
12 answers
619k views

Why are Python's 'private' methods not actually private?

Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: __myPrivateMethod(). How, then, can one explain this >&...
willurd's user avatar
  • 12.1k
18 votes
5 answers
8k views

Is it legal to redefine a C++ keyword?

In this article from Guru of the week, it is said: It is illegal to #define a reserved word. Is this true? I can’t find anything in the norm, and I have already seen programmers redefining new, for ...
qdii's user avatar
  • 13k
4 votes
4 answers
3k views

Python code/function layout

I am learning Python and am trying to figure out the best way to structure my code. Lets say I have a long function, and want to break it up into smaller functions. In C, I would make it a 'static' ...
Baruch's user avatar
  • 22k
7 votes
1 answer
1k views

Python import statements in complex package structures?

Consider the following hierarchy of three regular packages and their contents: quick ├── brown │ ├── fox.py │ └── __init__.py ├── lazy │ ├── dog.py │ └── __init__.py └── __init__.py Now ...
jasaarim's user avatar
  • 1,826
0 votes
1 answer
2k views

How to avoid importing module private functions?

I am asking about module privates, not class privates. Suggested in here, a module private starts with one underscore, such a element is not copied along when using the from <module_name> import ...
John's user avatar
  • 327
1 vote
1 answer
905 views

How to make a method ONLY accessible by other methods of same class and not outer world [in python]? [duplicate]

I have a class definition with two methods defined in it. The layout somewhat looks like below: class Sample: def calling-Method(self): print "Hi" calledMethod() def called-...
node_analyser's user avatar
0 votes
1 answer
454 views

Python: How to define variables in global space without defining them as globals

I am declaring a global list, and as part of the list element definition, I need to reference some obscure data from other modules. I would like to declare some simple short-hand references to these ...
Robert's user avatar
  • 525
1 vote
1 answer
120 views

Looking for a way to count the number of XML element appearances up to a certain point

I am new to python and xml, so maybe I'm not using the correct terms to find what I need but I looked around for a while on stackoverflow, and also tried reading the documentation for dom and mini-dom,...
TRA Knowledge's user avatar

15 30 50 per page