Python | Canvas in Kiwi using .kv file

Learn examples from DataCamp guru:

The following are the various places where _ is used in Python:

  1. Single underscore:
    • In the translator
    • After the name
    • Before name
  2. Double underscore:
    1. __leading_double_underscore
    2. __before after__

Single underscore

In the translator:
_ returns the value of the last executed expression value in Python Prompt / Interpreter

& gt; & gt; & gt; a = 10

& gt; & gt; & gt; b = 10

& gt; & gt; & gt; _
Traceback (most recent call last):

File "& lt; stdin & gt;" , line 1 , in & lt; module & gt; 

NameError: name `_` is not defined

& gt; & gt; & gt ; a + b

20
& gt; & gt; & gt; _
20

& gt; & gt; & gt; _ * 2

40
& gt; & gt; & gt; _
40

& gt; & gt; & gt; _ / 2

20

To ignore values:
On several occasions, we do not want the return values ​​at this time to assign these values ​​to Underscore. Used as a one-time variable.

# Ignore specific location / index value

for _ in rang ( 10 )

  print "Test"

 
# Ignore value when unpacking

a, b, _, _ = my_method (var1)

After the name
Python has its own default keywords that we cannot use as a variable name. To avoid this conflict between the python keyword and the variable, we use an underscore after the name

Example:

& gt; & gt; & gt;  class MyClass ():

.. .  def __ init __ ( self ):

...  print " OWK "

  

& gt; & gt; & gt;  def my_defination (var1 = 1 , class_ = MyClass):

...  print var1

...  print class_

  
& gt; & gt; & gt; my_defination ()
1
__ main __. MyClass
& gt; & gt; & gt; 

Before name
An underscore in front of a variable / function / method name indicates to the programmer that it is for internal use only and can be changed whenever the class wants.

Here, the name prefix through an underscore is considered private. If you specify from Import * all name starts with _ will not import. Python does not define really private, so it can be called directly from other modules, if specified in __all__, we also call it weak Private

class Prefix:

...  def __ init __ ( self ):

...  self . public = 10

...  self ._ private = 12

& gt; & gt; & gt; test = Prefix ()

& gt; & gt; & gt; test.public
10
& gt; & gt; & gt; test._private
12

Python class_file.py

def public_api ():

print "public api"

 

def _ private_api ( ):

print "private api "

Call file from hint

& gt; & gt; & gt;  from class_file import *

& gt; & gt; & gt; public_api ()
public api

 
& gt; & gt; & gt; _private_api ()
Traceback (most recent call last):

File "& lt; stdin & gt;" , line 1 , in & lt; module & gt; 

NameError: name `_private_api` is not defined

 

& gt; & gt; & gt;  import class_file

& gt; & gt; & gt ; class_file.public_api ()
public api
& gt; & gt; & gt; class_file._private_api ()
private api

Double underline (__)

__leading_double_underscore
Double underscore causes that the python interpreter rewrites the name to avoid conflict in the subclass. The interpreter changes the name of the variable with a class extension and this function known as Mangling. 
testFile.py

class Myclass ():

def __ init __ ( self ):

self .__ variable = 10

Call from translator

& gt; & gt; & gt;  import testFile

& gt; & gt; & gt ; obj = testFile.Myclass ()

& gt; & gt; & gt; obj .__ variable
Traceback (most recent call last):

File "", line 1 , in

AttributeError: Myclass instance has no attribute `__variable`

nce has no attribute `Myclass`

& gt; & gt; & gt; obj._Myclass__variable
10

In the Python Mangling interpreter, change the variable name with ___. So it is used as a private member several times because another class cannot access this variable directly. The main target for __ — use variable / method only in class. If you want to use it outside of the class, you can make the API publicly available

Call from per 

class Myclass ():

def __ init __ ( self ):

self .__ variable = 10

def func ( self )

print self .__ variable

& gt; & gt; & gt;  import testFile

& gt; & gt; & gt ; obj = testFile.Myclass ()

& gt; & gt; & gt; obj.func ()
10

__BEFORE AFTER__
A name starting with __ and ending with the same counts for special methods in Python. Python provides these methods in order to use it as a user-dependent overload operator.

Python provides this convention to distinguish a user-defined function from a module function

class Myclass ():

def __ add __ ( self , a, b):

print a * b

Call from translator

& gt; & gt; & gt;  import testFile

& gt; & gt; & gt ; obj = testFile.Myclass ()

& gt; & gt; & gt; obj .__ add __ ( 1 , 2 )

2

& gt; & gt; & gt ; obj .__ add __ ( 5 , 2 )

10

This article is courtesy of Nirmi Shah . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.

Learn examples from DataCamp guru: