-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path4-typeProof.py
More file actions
84 lines (60 loc) · 1.58 KB
/
4-typeProof.py
File metadata and controls
84 lines (60 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Typenprüfung
# ============
# Der Befehl type() aus crashcourse-0
#-------------------------------------
var1 = 10
var2 = 0.20
var3 = "Hallo"
print type(var1)
print type(var2)
print type(var3)
# type() und Klassen
#--------------------
class foo(object):
def __init__(self):
self._data = "Rot"
self.data = "blau"
print "Print inside Class: ", self._data
class foobar(object):
pass
# Instanziierung
fooClass = foo()
foobarClass = foobar()
print(type(fooClass))
# Typenprüfung mittels Type
if type(fooClass) == foo:
print "Jo fooClass is foo"
else:
print "Ups fooClass isn't foo"
if type(foobarClass) == foo:
print "Jo foobarClass is foo"
else:
print "Ups foobarClass isn't foo"
# Typenprüfung mittels isinstance
if isinstance(fooClass, foo):
print "Jo ...shit!!! it is foo"
else:
print "F! it isn't foo"
# Ableitungen von Klassen
class subfoo(foo):
def __init__(self):
super(subfoo, self).__init__()
self.subdata = "Grün"
classSubfoo = subfoo()
print "Subfoo Data: ", classSubfoo.data, classSubfoo.subdata
# Typenprüfung type vs isinstance
if type(classSubfoo) == foo:
print "by type Subfoo is foo"
else:
print "by type subfoo isn't foo"
if isinstance(classSubfoo, foo):
print "by isinstance Subfoo is foo"
else:
print "by isinstance subfoo isn't foo"
# und man kann auch auf die Elternklasse der Elternklasse prüfen
if isinstance(classSubfoo, object):
print "Jo, Subfoo Parent ist object"
else:
print "Subfoo ist not child of object"