Wednesday 10 May 2017

Python3

Built-In Class Attributes

Python class keeps following built-in attributes
  • __dict__: Dictionary containing the class's namespace.
  • __doc__: Class documentation string or none, if undefined.
  • __name__: Class name.
  • __module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode.
  • __bases__: A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
For the above class let us try to access all these attributes −
#!/usr/bin/python

class Employee:
   'Common base class for all employees'
   empCount = 0

   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1
   
   def displayCount(self):
     print "Total Employee %d" % Employee.empCount

   def displayEmployee(self):
      print "Name : ", self.name,  ", Salary: ", self.salary

print "Employee.__doc__:", Employee.__doc__
print "Employee.__name__:", Employee.__name__
print "Employee.__module__:", Employee.__module__
print "Employee.__bases__:", Employee.__bases__
print "Employee.__dict__:", Employee.__dict__
When the above code is executed, it produces the following result −
Employee.__doc__: Common base class for all employees
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: ()
Employee.__dict__: {'__module__': '__main__', 'displayCount':
<function displayCount at 0xb7c84994>, 'empCount': 2, 
'displayEmployee': <function displayEmployee at 0xb7c8441c>, 
'__doc__': 'Common base class for all employees', 
'__init__': <function __init__ at 0xb7c846bc>} 

Destroying Objects (Garbage Collection)

his __del__() destructor prints the class name of an instance that is about to be destroyed −
#!/usr/bin/python

class Point:
   def __init( self, x=0, y=0):
      self.x = x
      self.y = y
   def __del__(self):
      class_name = self.__class__.__name__
      print class_name, "destroyed"

pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3
When the above code is executed, it produces following result −
3083401324 3083401324 3083401324
Point destroyed

Overriding Methods

#!/usr/bin/python

class Parent:        # define parent class
   def myMethod(self):
      print 'Calling parent method'

class Child(Parent): # define child class
   def myMethod(self):
      print 'Calling child method'

c = Child()          # instance of child
c.myMethod()         # child calls overridden method

 

When the above code is executed, it produces the following result −
Calling child method

The match Function

re.match(pattern, string, flags=0)
Here is the description of the parameters:
ParameterDescription
patternThis is the regular expression to be matched.
stringThis is the string, which would be searched to match the pattern at the beginning of string.
flagsYou can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below.
#!/usr/bin/python
import re

line = "Cats are smarter than dogs"

matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)

if matchObj:
   print "matchObj.group() : ", matchObj.group()
   print "matchObj.group(1) : ", matchObj.group(1)
   print "matchObj.group(2) : ", matchObj.group(2)
else:
   print "No match!!"
When the above code is executed, it produces following result −
matchObj.group() :  Cats are smarter than dogs
matchObj.group(1) :  Cats
matchObj.group(2) :  smarter
 

The search Function

Here is the syntax for this function:
re.search(pattern, string, flags=0)
Here is the description of the parameters:
ParameterDescription
patternThis is the regular expression to be matched.
stringThis is the string, which would be searched to match the pattern anywhere in the string.
flagsYou can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below.
#!/usr/bin/python
import re

line = "Cats are smarter than dogs";

searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)

if searchObj:
   print "searchObj.group() : ", searchObj.group()
   print "searchObj.group(1) : ", searchObj.group(1)
   print "searchObj.group(2) : ", searchObj.group(2)
else:
   print "Nothing found!!"
When the above code is executed, it produces following result −
searchObj.group() :  Cats are smarter than dogs
searchObj.group(1) :  Cats
searchObj.group(2) :  smarter
match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string. 

 Search and Replace

 re.sub(pattern, repl, string, max=0)

 

#!/usr/bin/python
import re

phone = "2004-959-559 # This is Phone Number"

# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num

# Remove anything other than digits
num = re.sub(r'\D', "", phone)    
print "Phone Num : ", num
When the above code is executed, it produces the following result −
Phone Num :  2004-959-559
Phone Num :  2004959559

 

No comments:

Post a Comment