in Education by
What is the meaning of using leading underscores before the name of an object in Python? And what's the difference between a double leading underscore and a single leading underscore? I also want to know that does meaning stays same whether the object in the question is a function, variable, method, etc.? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Single underscore It is used before an object’s name to indicate other programmers that the method is meant to be private. It is also used in the name of methods which are fully intended to be overridden by subclasses. Ex- _abc Double underscore They are used for fully private methods. Ex- if your class is intended to be subclassed and you have attributes which you do not want the subclass to use, you can name them using a double underscore. The rules of underscores in Python remain the same whether it is an object or a function, etc. Let us see an example of class for better understanding of the difference between the single and double underscores. Example- >>> class ThisClass(): def __init__(self): self.__superprivate = "Orange" self._semiprivate = ", Apple" >>> my_class = ThisClass() >>> print my_class.__superprivate Traceback (most recent call last): File "", line 1, in AttributeError: thisClass instance has no attribute '__superprivate' >>> print my_class._semiprivate , Apple >>> print my_class.__dict__ {'_ThisClass__superprivate': 'Orange', '_semiprivate': ', Apple'}

Related questions

0 votes
    Can someone please tell me the basic difference between @classmethod and @staticmmethod, I am good with C++ so you ... we use them? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    What * and ** do for param2 in the following: def foo(param1, *param2): def bar(param1, **param2): Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How do I find out a name of class that created an instance of an object in Python if the function I am ... get at this information. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can I know if an object has some attribute in Python, Is there a way to do it? E.g. >>> q = ... property before using it in syntax? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone tell me a easy method to check if a variable is a dictionary, list or anything else? I want to know ... is of which type. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how can I Call a function in a string with function name in python program? E.x. Let's ... to perform this task. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to refer a null object in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    What is the meaning of _ after for in this code? if tbh.bag: n = 0 for _ in tbh.bag.atom_set(): n += 1 Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a list: foo= ['q'' , 'w' , 'e' , 'r' , 't' , 'y'] I want to retrieve an item randomly from this list, How can I do it in python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want to raise an exception in Python and in future want to detect it using an except block, Can someone tell me how to do it? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can someone get a index (1) in Python for a list ["qwe", "asd", "zxc"] and an item "asd" in the list? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
...