程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning notes_ Day08

編輯:Python

OOP: Object oriented programming

  • stay python in , Everything is the object
  • Objects have properties : Data attribute ( Variable ), Function attribute ( Method )
  • OOP When programming , We need to find out something in common , Abstract into a class class
  • In specific applications , Then create an instance based on the class
  • It is suggested to use the form of hump for class name , Such as MyClass
class GameRole:
def __init__(self, nm, wp):
self.name = nm
self.weapon = wp
if __name__ == '__main__':
lb = GameRole(' Lyu3 bu4 ', ' Fang Tian draws halberds ') # Create a lb Example
print(lb.name, lb.weapon)
  • __init__ Is a special method in a class , It's called the constructor method , It calls... Automatically when you create an instance
  • The first parameter of the method self, An example , Not a keyword , It can be any legal name
  • Variable name in method , It's a local variable of a function , Only available in this method
  • Variables bound to instances , Is an instance property , Can be applied anywhere in the class

Combine

  • The two classes are obviously different
  • One class is a component of another

Inherit

  • The two classes have a lot in common
  • One class is a subclass of another
  • Subclasses inherit the properties of the parent class
  • Father and son have the same name , Subclasses have high priority
  • A subclass can have more than one parent , Inherit all the methods of the parent class
  • When executing methods , The search order is from bottom to top , From left to right

Special methods

Class has some special methods that start and end with double underscores , Also known as magic Magic methods .

re modular

https://jex.im/regulex

# take mac Add a colon to the address
192.168.1.1 000C29123456
192.168.1.2 525400A31B2C
192.168.1.3 0002231A08D3
# Ideas : find mac Address 、 Two numbers in groups 、 Colon between groups
:%s/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)$/\1:\2:\3:\4:\5:\6/

re Module common methods

>>> import re
# match Match to , Return match object , Otherwise return to None
>>> re.match('f..', 'food')
<_sre.SRE_Match object; span=(0, 3), match='foo'>
>>> print(re.match('f..', 'seafood'))
None
# search Match in a string
>>> re.search('f..', 'food')
<_sre.SRE_Match object; span=(0, 3), match='foo'>
>>> re.search('f..', 'seafood')
<_sre.SRE_Match object; span=(3, 6), match='foo'>
>>> m = re.search('f..', 'seafood')
>>> m.group() # Match the object group Method returns the matching string
'foo'
# findall You can match everything
>>> re.findall('f..', 'seafood is food')
['foo', 'foo']
# finditer Returns the iterator of the matching object
>>> list(re.finditer('f..', 'seafood is food'))
[<_sre.SRE_Match object; span=(3, 6), match='foo'>, <_sre.SRE_Match object; span=(11, 14), match='foo'>]
>>> for m in re.finditer('f..', 'seafood is food'):
... m.group()
...
'foo'
'foo'
# split For cutting
# With . or - As a separator
>>> re.split('\.|-', 'how-are-you.tar.gz')
['how', 'are', 'you', 'tar', 'gz']
# Replace
# take X Replace with python
>>> re.sub('X', 'python', 'X is good. I like X.')
'python is good. I like python.'
# When there are a lot of matches , Compile the schema ahead of time , You can get better efficiency
>>> patt = re.compile('f..')
>>> patt.search('seafood')
<_sre.SRE_Match object; span=(3, 6), match='foo'>
>>> m = patt.search('seafood')
>>> m.group()
'foo'
>>> patt.findall('seafood is food')
['foo', 'foo']

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved