一个有趣的 Python 包,可以让任意属性调用都输出属性名称
Project description
eamin
一个有趣的 Python 包!🎉
🌟 功能特点
eamin 是一个神奇的 Python 包,它可以让你调用任何属性,并且会自动输出该属性的名称!
✨ 动态性 - 不需要预定义任何属性
🎯 通用性 - 对所有属性名都有效
🌍 国际化 - 支持任何 Unicode 字符
🎮 趣味性 - 展示 Python 元编程的强大
📦 安装
pip install eamin
🚀 快速开始
import eamin
# 输出 "hello_world"
eamin.hello_world(print)
# 输出 "printout"
eamin.printout(print)
# 对于任意属性都有相同的行为!
eamin.any_attribute_you_want(print) # 输出 "any_attribute_you_want"
eamin.你好世界(print) # 输出 "你好世界"
💡 使用示例
基础用法
import eamin
# 最简单的用法
eamin.test(print) # 输出: test
自定义输出函数
def shout(text):
print(f"🔊 {text.upper()}!!!")
eamin.exciting(shout) # 输出: 🔊 EXCITING!!!
获取属性名作为字符串
# 获取属性名
name = eamin.variable_name(lambda x: x)
print(name) # "variable_name"
# 转换为大写
upper = eamin.hello(lambda x: x.upper())
print(upper) # "HELLO"
# 获取长度
length = eamin.long_name(lambda x: len(x))
print(length) # 9
批量操作
# 动态生成配置键
config_keys = ['host', 'port', 'username', 'password']
config = {k: getattr(eamin, k)(str) for k in config_keys}
# {'host': 'host', 'port': 'port', 'username': 'username', 'password': 'password'}
支持多语言
eamin.hello(print) # hello
eamin.你好(print) # 你好
eamin.مرحبا(print) # مرحبا
eamin.Привет(print) # Привет
🔧 工作原理
eamin 使用 Python 的 __getattr__ 魔法方法和模块替换技术:
import sys
class EaminModule:
def __getattr__(self, name):
def magic_func(func=print):
return func(name)
return magic_func
sys.modules[__name__] = EaminModule()
当你访问任何属性时:
__getattr__拦截属性访问- 返回一个接受函数参数的闭包
- 调用该函数并传入属性名
🎪 为什么有趣?
- 展示 Python 元编程的强大能力
- 学习
__getattr__和模块系统 - 创造性地使用 Python 特性
- 简洁优雅的代码实现复杂功能
📚 更多示例
import eamin
# 链式处理
results = []
for name in ['apple', 'banana', 'cherry']:
result = getattr(eamin, name)(lambda x: x.upper())
results.append(result)
print(results) # ['APPLE', 'BANANA', 'CHERRY']
# 反转字符串
reversed_text = eamin.test(lambda x: x[::-1])
print(reversed_text) # "tset"
# 自定义格式化
def fancy_print(text):
print(f"✨ {text} ✨")
eamin.magic(fancy_print) # ✨ magic ✨
⚠️ 注意事项
这个包主要用于:
- 学习 Python 元编程
- 娱乐和实验
- 展示 Python 的灵活性
不建议用于生产环境的关键代码。
📖 许可证
MIT License - 自由使用!
🤝 贡献
欢迎提交 Issues 和 Pull Requests!
🔗 链接
- GitHub: https://github.com/yourusername/eamin
- PyPI: https://pypi.org/project/eamin/
- 问题反馈: https://github.com/yourusername/eamin/issues
用 Python 玩得开心!🐍✨
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file eamin-0.1.0.tar.gz.
File metadata
- Download URL: eamin-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95edcb1b70de51213a50c0aa0aeb2b15cbf6883c81846a897e30f5aa89a9c0c3
|
|
| MD5 |
fedf0a8932e49c7d8cb2f4ba0e71084b
|
|
| BLAKE2b-256 |
d9c1877fa4a674198219f809021711e2be37b2e176b204e448b1a6a456a3068c
|
File details
Details for the file eamin-0.1.0-py3-none-any.whl.
File metadata
- Download URL: eamin-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8086fc3740847091b7f75e8343c2096fdf125c63658973c82d2a52019dc82f1
|
|
| MD5 |
c93c10ad062972569b38b206bfae3927
|
|
| BLAKE2b-256 |
743f6ae8c08f3a157406e6ac0ee7b5e8cf03e20509c51253b1420459de3b30b4
|