hausaufgabe--python 39 -- objects and class

00-- 

class, class object, instance object

 

As shown below,  Tracking is a class, and itself it's own class oject. in the object, __dict__ will have all the original data;  d is the instance object of Tracking class with value (2,3), and __dict__ data for d is unique for d. the class object's data in __dict__ still the same for the next newly created instance object. 

 

001-- to add a attribute in the Class to trace how many related objects have been created :

class Tracking:
    count = 0

    def __init__(self,x,y):
        self.x = x
        self.y = y
        Tracking.count += 1
    def __del__(self):
        Tracking.count -= 1
    
   
    
       
  

    
        

 

Running result:

 

 

002-- define a Stack class with below functions

a-- isEmpty() to check if the Stack is empty (return True or False)

b-- push() add a data from top 

c -- pop() pop a data from top

d -- top() to show a data from top

d -- bottom() to show a data of bottom 

 

class Stack(list):
    count = 0
    def __init__(self):
        Stack.count +=1
        self = []
    def __del__(self):
        Stack.count -=1

    def isEmpty(self):
        return not len(self) # to check if any data in it

    def push(self,x):
        self.append(x) #to add a data from top
        print('data have been added')
        
    def top(self):
        return self[len(self)-1]#to show the data of top

    def bottom(self):
        return self[0]#to show the data in the bottom
        

 

 

Standar:

class Stack:
    def __init__(self, start=[]):
        self.stack = []
        for x in start:
            self.push(x)

    def isEmpty(self):
        return not self.stack
    
    def push(self, obj):
        self.stack.append(obj)
 
    def pop(self):
        if not self.stack:
            print('Warming: Stack is empty!')
        else:
            return self.stack.pop()
 
    def top(self):
        if not self.stack:
            print('Warming: Stack is empty!')
        else:
            return self.stack[-1]
 
    def bottom(self):
        if not self.stack:
            print('Warming: Stack is empty!')
        else:
            return self.stack[0]

 

003-- class combination

// turtle class
class Turtle:
    def __init__(self, x):
        self.num = x
// Fish class
class Fish:
    def __init__(self, x):
        self.num = x
// Pool class
class Pool:
    def __init__(self, x, y):
        self.turtle = Turtle(x)        // combine with class Turtle
        self.fish = Fish(y)        //  combine with class Fish
     
    def print_num(self):
        print("There are Turle %d ,Fish %d in the Pool!" % (self.turtle.num, self.fish.num))

>>> pool = Pool(1, 10)
>>> pool.print_num()

 

004-- please note that if the attribute's name is the name as function's, then it will overwrite function. 

class C:
        def x(self):
                print('Xman')

>>> c = C()
>>> c.x()
Xman
>>> c.x = 1
>>> c.x
1
>>> c.x()
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    c.x()
TypeError: 'int' object is not callable

 

 

posted @ 2017-09-07 22:41  ReedyLi  阅读(236)  评论(0编辑  收藏  举报