重构第一版:重新验证2.0推荐写法
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
from typing import Annotated
|
||||
|
||||
|
||||
def say_hello(name: Annotated[str, "this is just metadata"]) -> str:
|
||||
return f"Hello {name}"
|
||||
|
||||
print(say_hello('Annotated'))
|
||||
@@ -0,0 +1,24 @@
|
||||
class Class:
|
||||
name = 'Class'
|
||||
|
||||
def __init__(self, at):
|
||||
self.attribute = at
|
||||
|
||||
def change(self, ano):
|
||||
self.attribute = ano
|
||||
self.name = ano
|
||||
self.__class__.name = ano
|
||||
|
||||
|
||||
def change_obj(self, ano):
|
||||
self.attribute = ano
|
||||
self.name = ano
|
||||
|
||||
|
||||
clazz = Class('abc')
|
||||
print(clazz.name)
|
||||
|
||||
# clazz.change('good')
|
||||
# print(Class.name, clazz.name, clazz.attribute)
|
||||
clazz.change_obj('good')
|
||||
print(Class.name, clazz.name, clazz.attribute)
|
||||
@@ -0,0 +1,24 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
id: int
|
||||
name: str = "John Doe"
|
||||
signup_ts: datetime | None = None
|
||||
friends: list[int] = []
|
||||
|
||||
|
||||
external_data = {
|
||||
"id": "123",
|
||||
"signup_ts": "2017-06-01 12:22",
|
||||
"friends": [1, "2", b"3"],
|
||||
}
|
||||
user = User(**external_data)
|
||||
print(user)
|
||||
# > User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3]
|
||||
print(user.id)
|
||||
# > 123
|
||||
|
||||
print(type(user.friends[0]),type(user.friends[1]),type(user.friends[2]))
|
||||
Reference in New Issue
Block a user