Movatterモバイル変換
[0]ホーム
dynamic casting in python
Rainer Deykeroot at rainerdeyke.com
Fri Apr 20 13:14:46 EDT 2001
"Christian Reyes" <christian at rocketnetwork.com> wrote in messagenews:9bo76f$q82$1 at bob.news.rcn.net...> How do I do it?> Is it possible?def dynamic_cast(klass, object): if isinstance(object, klass): return object # Returns 'None' if 'object' is not an instance of 'klass'class C: passclass A(C): passclass B(C): passdef create_C(name): # Factory function if name == 'A': return A() if name == 'B': return B() raise ValueError, 'create_C argument must be "A" or "B"'spam = create_C('A')spam_as_A = dynamic_cast(A, spam) # Sets 'spam_as_A' to 'spam'.spam_as_B = dynamic_cast(B, spam) # Sets 'spam_as_B' to 'None'.--Rainer Deyke (root at rainerdeyke.com)Shareware computer games -http://rainerdeyke.com"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-listmailing list
[8]ページ先頭