gznz12345 发表于 2018-8-16 06:27:08

面向对象 Python的类 介绍

#!/usr/bin/env python  
# -*- coding:UTF-8 -*-
  

  
class Test_N:
  

  
    """ class get a name and count string or number """
  

  
    def __init__(self, n):
  
      self.name = n
  

  
    def sqrt_num(self, cn):
  
      if cn >= 0:
  
            root = cn ** 0.5
  
            return -root, root
  
      else:
  
            print "negative number."
  
            return
  

  
    def c_str(self, cs):
  
      return len(cs)
  

  
    @staticmethod
  
    def square_list( *args ):
  
      r = []
  
      for i in args:
  
            r.append(i ** 2)
  
      return r
  

  
    def genome_len(self, fasta):
  
      return len(fasta)
  

  
    def anno_genome(self , gff):
  
      handle = open(gff, 'r')
  
      n = 0
  
      for i in handle:
  
            n += 1
  
      handle.close()
  
      return n
  

  

  

  

  
x = Test_N("Kate")      # initiate the class
  
print x.name
  

  
print "Kate\'s number is 1235678."
  

  
print "It\'s square root are:"
  
print x.sqrt_num(1235678)# invoke the function sqrt_num
  

  
print "Kate\'s string is agahccp."
  
print "It\'s count is:"
  
print x.c_str("agahccp")   # invoke the function c_str
  

  
for i in Test_N.square_list(3,4,5,6,7):   # invoke the method squre_list
  
    print i
  

  
print x.anno_genome("log.txt")    # invoke the function anno_genome
页: [1]
查看完整版本: 面向对象 Python的类 介绍