python: create object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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())

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 @   ®Geovin Du Dream Park™  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2023-10-12 typescript: Observer Pattern
2023-10-12 typescript: Mediator pattern
2023-10-12 typesciprt: Command Pattern
2022-10-12 CSharp: Simple Factory Pattern in donet core 3
2022-10-12 CSharp: Interpreter Pattern in donet core 3
2022-10-12 CSharp: Chain of Responsibility Pattern in donet core 3
2019-10-12 css: hide or dispaly div
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示