A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2001-April/063731.html below:

dynamic casting in python

dynamic casting in python dynamic casting in pythonRainer Deyke root at rainerdeyke.com
Fri Apr 20 13:14:46 EDT 2001
"Christian Reyes" <christian at rocketnetwork.com> wrote in message
news: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: pass
class A(C): pass
class B(C): pass

def 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-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4