Tuesday, June 15, 2021

Python 3 - refresh concepts 2

Data Hiding

An object's attributes may or may not be visible outside the class definition. You need to name attributes with a double underscore prefix, and those attributes then will not be directly visible to outsiders.


#!/usr/bin/python3


class JustCounter:

   __secretCount = 0

  

   def count(self):

      self.__secretCount += 1

      print (self.__secretCount)


counter = JustCounter()

counter.count()

counter.count()

print (counter.__secretCount)



Python protects those members by internally changing the name to include the class name. You can access such attributes as object._className__attrName. If you would replace your last line as following, then it works for you −

.........................

print (counter._JustCounter__secretCount)



Python Environment Variables

Here are important environment variables, which are recognized by Python −



PYTHONPATH

It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.


PYTHONSTARTUP

It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.



PYTHONCASEOK

It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.


PYTHONHOME

It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.



** Exponent

Performs exponential (power) calculation on operators

a**b =10 to the power 20

//

Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):

9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0


Floor division is a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result. Floor function is mathematically denoted by this symbol.



You can also delete the reference to a number object by using the del statement. The syntax of the del statement is −

del var1[,var2[,var3[....,varN]]]]

You can delete a single object or multiple objects by using the del statement. For example −

del var

del var_a, var_b



It is possible to represent an integer in hexa-decimal or octal form

>>> number = 0xA0F #Hexa-decimal

>>> number

2575


>>> number = 0o37 #Octal

>>> number

31


No comments:

Post a Comment