python: create object

import asyncio
import asynchat

class Queue(object):
    """
    
    """
    def __init__(self):
        """
        
        """
        
        self.queue=[]
    
    def export(self,data):
        """
        
        :param data: 
        :return: 
        """
        self.queue.append(data)
        
    def defimport(self):
        """
        
        :return: 
        """
        if len(self.queue):
            return self.queue.pop()
        return 'empty'
        
    
    def size(self):
        """
        
        :return: 
        """
        return len(self.queue)  

#动态代码的执行
a=[1,5,10]
exec('for i in a: print(i)')

async def greeting(name:str):
   print(f'hello,{name}')
   

asyncio.run(greeting('geovindu'))

async  def main():
     for name in ['geovindu','paulo','sida']:
        a=await greeting(name)
        if a!=None:
           print(a)
           
if __name__=="__main__":   
     asyncio.run(main())
     q=Queue()
     q.export('a')
     q.export('b')
     q.export('c')
     print(q.defimport())
     print(q.defimport())
     print(q.defimport())
     print(q.defimport())

  

 

 

class Account(object):
    """
    账户
    """
    owner:str  #类型提示
    """
    类型提示
    """
    balnace:float #类型提示
    """
    类型提示
    """
    
    def __init__(self, owner:any, balance:any):
        self.owner = owner
        self.balnace = balance
    
    def __repr__(self):
        """
        
        :return: 
        """
        return f'Acount({self.owner!r},{self.balnace!r})'
    
    def deposit(self,amount):
        """
        
        :param amount: 
        :return: 
        """
        self.balnace += amount
    
    def withdraw(self,amount):
        """
        
        :param amount: 
        :return: 
        """
        self.balnace -= amount
        
    def inquiry(self):
        """
        
        :return: 
        """
        return self.balnace
    
    
#test
b=Account("Du",10.0)
print(b.inquiry())
b.deposit(50)
print(b.inquiry())
b.withdraw(10)
print(b.inquiry())
print(b)

output:
10.0
60.0
50.0
Acount('Du',50.0)

  

2.

class Account(object):
    """
    账户
    """
    owner: str  # 类型提示
    """
    类型提示
    """
    balnace: float  # 类型提示
    """
    类型提示
    """

    def __init__(self):
        self._owner:str = any
        self._balnace:float = any

    def __repr__(self):
        """

        :return:
        """
        return f'Acount({self._owner!r},{self._balnace!r})'

    @property
    def owner(self):
        """

        :return:
        """
        return self._owner

    @owner.setter
    def owner(self,owner):
        """

        :param owner:
        :return:
        """
        if not isinstance(owner,str):
            raise TypeError('Exepcted str')
        if len(owner)>10:
            raise ValueError('Must be 10 Characters or less')
        self._owner=owner
    @property
    def balnace(self):
        """

        :return:
        """
        return self._balnace

    @balnace.setter
    def balnace(self,balnace):
        """

        :param balnace:
        :return:
        """

        self._balnace=balnace
    @property
    def deposit(self):
        """

        :return:
        """
        return self._balnace

    @deposit.setter
    def deposit(self, amount):
        """

        :param amount:
        :return:
        """
        self._balnace += amount

    @property
    def withdraw(self):
        """

        :return:
        """
        return self._balnace

    @withdraw.setter
    def withdraw(self, amount):
        """

        :param amount:
        :return:
        """
        self._balnace -= amount

    @property
    def inquiry(self):
        """

        :return:
        """
        return self._balnace

    @owner.deleter
    def owner(self):
        """
        
        :return:
        """
        print('deleting')



    
    
#test
b=Account()
b.owner='eve'
b.balnace=10
print(b.inquiry)
b.deposit=50
print(b.inquiry)
b.withdraw=10
print(b.inquiry)
print(b)

  

3

class Account(object):  
    """  
    账户  
    """  
    owner: str  # 类型提示  
    balnace: float  # 类型提示  
    cls_balance = 0  # 类变量,用于跟踪所有账户的余额总和  
  
    def __init__(self):  
        self._owner = any  
        self._balnace = any
  
    def __repr__(self):  
        return f'\nAccount({self._owner!r}, {self._balnace!r})'  
  
    @property  
    def owner(self):  
        return self._owner  
  
    @owner.setter  
    def owner(self, owner: str):  
        if not isinstance(owner, str):  
            raise TypeError('Expected str')  
        if len(owner) > 10:  
            raise ValueError('Must be 10 characters or less')  
        self._owner = owner  
  
    @property  
    def balance(self):  
        return self._balnace  
  
    @balance.setter  
    def balance(self, amount: float):  
        self._balnace = amount  
  
    @classmethod  
    def deposit(cls, amount: float):  
        cls.cls_balance += amount  
        print(type(cls.cls_balance))  # Debugging print, can be removed  
  
    @classmethod  
    def withdraw(cls, amount: float):  
        cls.cls_balance -= amount  
  
    @property  
    def balance_inquiry(self):  
        return self.balance  
  
    @owner.deleter  
    def owner(self):  
        print('Deleting owner')
        
    def toString(cls):
        """
        """
        print(cls.cls_balance)
  
# 测试  
b = Account()  
b.owner = 'eve'  
b.balance = 10  # Uncommented for testing  
Account.deposit(10)  
print(b.owner,Account.cls_balance)  
print('\n' + str(b.balance_inquiry))  
Account.deposit(50)  
print(b.owner,Account.cls_balance)  
Account.withdraw(10)  
print(b.owner,Account.cls_balance)  
print(b)
b.toString()
print()

  

posted @ 2024-10-12 22:51  ®Geovin Du Dream Park™  阅读(7)  评论(0编辑  收藏  举报