Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Subtyping
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Python=== The following python code establishes an explicit inheritance relationship between classes {{var|B}} and {{var|A}}, where {{var|B}} is both a subclass and a subtype of {{var|A}}, and can be used as an {{var|A}} wherever a {{var|B}} is required. <syntaxhighlight lang="python"> class A: def do_something_a_like(self): pass class B(A): def do_something_b_like(self): pass def use_an_a(some_a): some_a.do_something_a_like() def some_func(): b = B() use_an_a(b) # b can be substituted for an A. </syntaxhighlight> The following example, {{var|type(a)}} is a "regular" type, and {{var|type(type(a))}} is a metatype. While as distributed all types have the same metatype ({{var|PyType_Type}}, which is also its own metatype), this is not a requirement. The type of classic classes, known as {{var|types.ClassType}}, can also be considered a distinct metatype.<ref>{{cite web|author=Guido van Rossum|title=Subtyping Built-in Types|url=https://www.python.org/dev/peps/pep-0253/|access-date=2 October 2012}}</ref> <syntaxhighlight lang="pycon"> >>> a = 0 >>> type(a) <type 'int'> >>> type(type(a)) <type 'type'> >>> type(type(type(a))) <type 'type'> >>> type(type(type(type(a)))) <type 'type'> </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)