Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to make the effect of sync_attr and sync_attrs permanent #168

Merged
merged 1 commit into from
Mar 16, 2025

Conversation

gottadiveintopython
Copy link
Member

sync_attrsync_attrsはこれまでcontext managerとしてしか使えなかったが、普通の函数としても使えるようにする。
context managerとして使った場合は効果はwith-blockが終わるまでの一時的なものだが、普通の函数として使えば__exit()__が自動では呼ばれなくなるため効果は永続的になる。

# 効果はwith-blockが終わるまで
with sync_attr(...):
    ...

# 効果が永続
sync_attr(...)

加えて直ちに値の同期を行うようにもした。

import types
w = Widget(x=0)
obj = types.SimpleNamespace()

with sync_attr((w, 'x'), (obj, 'xx')):
    # assert not hasattr(obj, 'xx')  # このPR前はこう
    assert w.x is obj.xx  # このPR後はこう

…_attrs' permanent.
@gottadiveintopython gottadiveintopython merged commit 01a2390 into main Mar 16, 2025
5 checks passed
@gottadiveintopython gottadiveintopython deleted the sync_attr branch March 16, 2025 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant