hausaufgabe--python 39 -- objects and class

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

技术分享

 

以上是关于hausaufgabe--python 39 -- objects and class的主要内容,如果未能解决你的问题,请参考以下文章

Hausaufgabe--Python 05

hausaufgabe--python 26 -Dict 2

hausaufgabe--python 16- Sequenz

Hausaufgabe--Python 04

hausaufgabe--python 22- Recurse

Hausaufgabe--Python 02