Last active
August 29, 2015 14:23
-
-
Save edelbluth/7423e2e34bcd7447b1be to your computer and use it in GitHub Desktop.
juergen.rocks > Python-Klassen-Initialisierung: Bemerkenswertes Verhalten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright 2015 Juergen Edelbluth | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
__author__ = 'Juergen Edelbluth' | |
class A(object): | |
class ASub: | |
A_SUB_VALUE = 1 | |
def __init__(self, a=ASub.A_SUB_VALUE): | |
self.__a = a | |
@property | |
def a(self): | |
return self.__a | |
class B(object): | |
class BSub: | |
B_SUB_VALUE = 1 | |
@classmethod | |
def get_b(cls): | |
return cls.B_SUB_VALUE | |
def __init__(self, b=BSub.get_b()): | |
self.__b = b | |
@property | |
def b(self): | |
return self.__b | |
class C(object): | |
class CSub: | |
C_SUB_VALUE = 1 | |
def __init__(self, c=None): | |
self.__c = c | |
if c is None: | |
self.__c = C.CSub.C_SUB_VALUE | |
@property | |
def c(self): | |
return self.__c | |
class D(object): | |
class DSub: | |
D_SUB_VALUE = 1 | |
@classmethod | |
def get_d(cls): | |
return cls.D_SUB_VALUE | |
def __init__(self, d=None): | |
self.__d = d | |
if d is None: | |
self.__d = D.DSub.get_d() | |
@property | |
def d(self): | |
return self.__d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright 2015 Juergen Edelbluth | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
__author__ = 'Juergen Edelbluth' | |
class AValue(object): | |
A_VALUE = 1 | |
class A(object): | |
def __init__(self, a=AValue.A_VALUE): | |
self.__a = a | |
@property | |
def a(self): | |
return self.__a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright 2015 Juergen Edelbluth | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
__author__ = 'Juergen Edelbluth' | |
from unittest import TestCase | |
import nested | |
import plain | |
class NestedTest(TestCase): | |
def test_a(self): | |
n1 = nested.A() | |
self.assertEqual(n1.a, 1) | |
nested.A.ASub.A_SUB_VALUE = 10 | |
n2 = nested.A() | |
self.assertEqual(n2.a, 1) | |
n3 = nested.A(100) | |
self.assertEqual(n3.a, 100) | |
def test_b(self): | |
n1 = nested.B() | |
self.assertEqual(n1.b, 1) | |
nested.B.BSub.B_SUB_VALUE = 10 | |
n2 = nested.B() | |
self.assertEqual(n2.b, 1) | |
n3 = nested.B(100) | |
self.assertEqual(n3.b, 100) | |
def test_c(self): | |
n1 = nested.C() | |
self.assertEqual(n1.c, 1) | |
nested.C.CSub.C_SUB_VALUE = 10 | |
n2 = nested.C() | |
self.assertEqual(n2.c, 10) | |
n3 = nested.C(100) | |
self.assertEqual(n3.c, 100) | |
def test_d(self): | |
n1 = nested.D() | |
self.assertEqual(n1.d, 1) | |
nested.D.DSub.D_SUB_VALUE = 10 | |
n2 = nested.D() | |
self.assertEqual(n2.d, 10) | |
n3 = nested.D(100) | |
self.assertEqual(n3.d, 100) | |
class PlainTest(TestCase): | |
def test_a(self): | |
p1 = plain.A() | |
self.assertEqual(p1.a, 1) | |
plain.AValue.A_VALUE = 10 | |
p2 = plain.A() | |
self.assertEqual(p2.a, 1) | |
p3 = plain.A(100) | |
self.assertEqual(p3.a, 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment