Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
class Person:
def __init__(self, name):
self.__name = name
self.__partner = None
def getName(self): return self.__name
def __str__(self):
return self.__name + "<" + (self.__partner.getName() if self.__partner else "None") + ">"
def marry(self, partner):
u""" 結婚 """
if self.__partner: return
self.__partner = partner
partner.marry(self)
def divorce(self):
u""" 離婚 """
if not self.__partner: return
partner = self.__partner
self.__partner = None
partner.divorce()
tarou = Person("Tarou")
hanako = Person("Hanako")
# 結婚
tarou.marry(hanako)
print tarou, hanako #=> Tarou:Hanako Hanako:Tarou
# 離婚
hanako.divorce()
print tarou, hanako #=> Tarou:None Hanako:None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment