Python Advanced---Object-oriented third projectile (Advanced article)

Source: Internet
Author: User

Some methods in Python objects

First, __str__

Class Teacher:
def __init__ (self,name,age):
Self.name=name
Self.age=age
Self.courses=[]

def teach (self):
Print ('%s teach '%self.name)

def __str__ (self):
Return ' <name:%s age:%s> '% (self.name,self.age)

Class Course:
def __init__ (self,name,price,period):
Self.name=name
Self.price=price
Self.period=period
def __str__ (self):
Return ' name:%s price:%s period:%s '% (self.name,self.price,self.period)

# egon=teacher (' Egon ', 18)
# print (Egon) #egon. __str__ ()
# print (Egon) #egon. __str__ ()

Ii. usage of __del__

Import time
# class Foo:
# def __init__ (self,x):
# self.x=x
# print (' Connect MySQL ') #conn =abcdef (' 192.168.1.10 ', 3306)
#
# def __del__ (self):
# ' Do some cleanup operations related to this object '
# # Conn.close ()
# # File.close ()
# print (' ====> ')
# F=foo (10)
# del F #f. __del__ ()
# Time.sleep (3)
# print (' main program ')

Third, item usage

Methods such as dic[' a '] can be implemented

# l=[' A ', ' B ', ' C ')
# dic={' a ': 1}
#
# print (l[1])
# Print (dic[' a '])

Class Foo:
def __init__ (self,name,age,sex):
Self.name=name
Self.age=age
Self.sex=sex
def __getitem__ (self, item):
# Print (Self,item,type (item))
# return GetAttr (Self,item)
return Self.__dict__[item]
def __setitem__ (self, Key, value):
# setattr (Self,key,value)
Self.__dict__[key]=value

def __delitem__ (self, key):
# delattr (Self,key)
Self.__dict__.pop (Key)

def __len__ (self):
Return 10
F=foo (' Egon ', ' Male ')
# print (f.name) #f [' name ']
# print (f.age) #f [' Age ']
# print (f.sex) #f [' Sex ']

# print (f[' name ')

# f[' name ']= ' EGON_NB '
# Print (f.__dict__)
# del f[' name ']
# Print (f.__dict__)

Print (Len (f))

Iv. Isinstance and Issubclass

Isinstance (obj,cls) checks if obj is an object of class CLS

Class Foo (object):
Pass

obj = Foo ()

Isinstance (obj, Foo)
Issubclass (sub, super) check if the sub class is a derived class of super class


Class Foo (object):
Pass

Class Bar (Foo):
Pass

Issubclass (Bar, Foo)

V. Reflection

Class Teacher:
# school= ' Oldboy '
# def __init__ (self,name,age):
# Self.name=name
# Self.age=age
#
# def teach (self):
# print ('%s teach '%self.name)


# Print (Teacher.school)
# Print (teacher.__dict__[' school ')

# Print (hasattr (Teacher, ' School '))

# Print (GetAttr (Teacher, ' School '))
# Print (GetAttr (Teacher, ' solasdf ', None))


# teacher.x=123
# setattr (Teacher, ' X ', 123)
# Print (teacher.x)


# delattr (Teacher, ' school ')
# Print (Teacher.school)


#对象
# t=teacher (' Egon ', 18)
# print (hasattr (t, ' name ')) #判断对象是否有name属性("in string mode" )

# print (GetAttr (t, ' name ')) #获取对象的name属性("in string mode" )

# setattr (t, ' sex ', ' Male ') #修改对象的sex属性 ("In the Way of strings")
# print (GetAttr (t, ' sex '))
#
# Print (t.__dict__)
# delattr (t, ' name ')
# Print (t.__dict__)

# T.teach ()
# Print (T.school)

# print (GetAttr (t, ' teach '))
# print (GetAttr (t, ' School '))
# t.school= ' Hahahahahahahahahhahahahahahhahahahahahahh '
# Print (t.__dict__)

Python Advanced---Object-oriented third projectile (Advanced article)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.