Skip to content

Commit 93a7d5f

Browse files
committed
Updated my original python3 answers to correspond with language changes that break that tests
1 parent aa94d5e commit 93a7d5f

File tree

5 files changed

+70
-81
lines changed

5 files changed

+70
-81
lines changed

python 3/koans/a_normal_folder/a_module.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

python 3/koans/about_class_attributes.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,52 @@
1010
class AboutClassAttributes(Koan):
1111
class Dog:
1212
pass
13-
13+
1414
def test_objects_are_objects(self):
1515
fido = self.Dog()
1616
self.assertEqual(True, isinstance(fido, object))
1717

1818
def test_classes_are_types(self):
1919
self.assertEqual(True, self.Dog.__class__ == type)
20-
20+
2121
def test_classes_are_objects_too(self):
2222
self.assertEqual(True, issubclass(self.Dog, object))
23-
23+
2424
def test_objects_have_methods(self):
2525
fido = self.Dog()
26-
self.assertEqual(24, len(dir(fido)))
27-
26+
self.assertEqual(26, len(dir(fido)))
27+
2828
def test_classes_have_methods(self):
29-
self.assertEqual(24, len(dir(self.Dog)))
29+
self.assertEqual(26, len(dir(self.Dog)))
3030

3131
def test_creating_objects_without_defining_a_class(self):
32-
singularity = object()
33-
self.assertEqual(21, len(dir(singularity)))
32+
singularity = object()
33+
self.assertEqual(22, len(dir(singularity)))
3434

3535
def test_defining_attributes_on_individual_objects(self):
3636
fido = self.Dog()
3737
fido.legs = 4
38-
38+
3939
self.assertEqual(4, fido.legs)
40-
40+
4141
def test_defining_functions_on_individual_objects(self):
4242
fido = self.Dog()
4343
fido.wag = lambda : 'fidos wag'
44-
44+
4545
self.assertEqual('fidos wag', fido.wag())
4646

4747
def test_other_objects_are_not_affected_by_these_singleton_functions(self):
4848
fido = self.Dog()
4949
rover = self.Dog()
5050

5151
def wag():
52-
return 'fidos wag'
52+
return 'fidos wag'
5353
fido.wag = wag
54-
54+
5555
with self.assertRaises(AttributeError): rover.wag()
56-
56+
5757
# ------------------------------------------------------------------
58-
58+
5959
class Dog2:
6060
def wag(self):
6161
return 'instance wag'
@@ -69,14 +69,14 @@ def growl(self):
6969
@staticmethod
7070
def bark():
7171
return "staticmethod bark, arg: None"
72-
72+
7373
@classmethod
7474
def growl(cls):
75-
return "classmethod growl, arg: cls=" + cls.__name__
76-
75+
return "classmethod growl, arg: cls=" + cls.__name__
76+
7777
def test_since_classes_are_objects_you_can_define_singleton_methods_on_them_too(self):
7878
self.assertRegexpMatches(self.Dog2.growl(), 'classmethod growl, arg: cls=Dog2')
79-
79+
8080
def test_classmethods_are_not_independent_of_instance_methods(self):
8181
fido = self.Dog2()
8282
self.assertRegexpMatches(fido.growl(), 'classmethod growl, arg: cls=Dog2')
@@ -88,9 +88,9 @@ def test_staticmethods_are_unbound_functions_housed_in_a_class(self):
8888
def test_staticmethods_also_overshadow_instance_methods(self):
8989
fido = self.Dog2()
9090
self.assertRegexpMatches(fido.bark(), 'staticmethod bark, arg: None')
91-
91+
9292
# ------------------------------------------------------------------
93-
93+
9494
class Dog3:
9595
def __init__(self):
9696
self._name = None
@@ -100,37 +100,37 @@ def get_name_from_instance(self):
100100

101101
def set_name_from_instance(self, name):
102102
self._name = name
103-
104-
@classmethod
103+
104+
@classmethod
105105
def get_name(cls):
106106
return cls._name
107107

108-
@classmethod
108+
@classmethod
109109
def set_name(cls, name):
110110
cls._name = name
111-
111+
112112
name = property(get_name, set_name)
113113
name_from_instance = property(get_name_from_instance, set_name_from_instance)
114-
114+
115115
def test_classmethods_can_not_be_used_as_properties(self):
116116
fido = self.Dog3()
117117
with self.assertRaises(TypeError): fido.name = "Fido"
118-
118+
119119
def test_classes_and_instances_do_not_share_instance_attributes(self):
120120
fido = self.Dog3()
121121
fido.set_name_from_instance("Fido")
122122
fido.set_name("Rover")
123123
self.assertEqual('Fido', fido.get_name_from_instance())
124124
self.assertEqual('Rover', self.Dog3.get_name())
125-
125+
126126
def test_classes_and_instances_do_share_class_attributes(self):
127127
fido = self.Dog3()
128128
fido.set_name("Fido")
129129
self.assertEqual('Fido', fido.get_name())
130130
self.assertEqual('Fido', self.Dog3.get_name())
131-
131+
132132
# ------------------------------------------------------------------
133-
133+
134134
class Dog4:
135135
def a_class_method(cls):
136136
return 'dogs class method'
@@ -140,15 +140,15 @@ def a_static_method():
140140

141141
a_class_method = classmethod(a_class_method)
142142
a_static_method = staticmethod(a_static_method)
143-
143+
144144
def test_you_can_define_class_methods_without_using_a_decorator(self):
145145
self.assertEqual('dogs class method', self.Dog4.a_class_method())
146146

147147
def test_you_can_define_static_methods_without_using_a_decorator(self):
148148
self.assertEqual('dogs static method', self.Dog4.a_static_method())
149-
149+
150150
# ------------------------------------------------------------------
151-
151+
152152
def test_heres_an_easy_way_to_explicitly_call_class_methods_from_instance_methods(self):
153153
fido = self.Dog4()
154154
self.assertEqual('dogs class method', fido.__class__.a_class_method())

python 3/koans/about_method_bindings.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,40 @@ def function2():
1111

1212
class Class:
1313
def method(self):
14-
return "parrot"
14+
return "parrot"
1515

1616
class AboutMethodBindings(Koan):
17-
def test_methods_are_bound_to_an_object(self):
17+
def test_methods_are_bound_to_an_object(self):
1818
obj = Class()
1919
self.assertEqual(True, obj.method.__self__ == obj)
2020

2121
def test_methods_are_also_bound_to_a_function(self):
2222
obj = Class()
2323
self.assertEqual('parrot', obj.method())
2424
self.assertEqual('parrot', obj.method.__func__(obj))
25-
25+
2626
def test_functions_have_attributes(self):
2727
obj = Class()
28-
self.assertEqual(32, len(dir(function)))
28+
self.assertEqual(34, len(dir(function)))
2929
self.assertEqual(True, dir(function) == dir(obj.method.__func__))
30-
30+
3131
def test_methods_have_different_attributes(self):
3232
obj = Class()
33-
self.assertEqual(25, len(dir(obj.method)))
33+
self.assertEqual(26, len(dir(obj.method)))
3434

3535
def test_setting_attributes_on_an_unbound_function(self):
3636
function.cherries = 3
3737
self.assertEqual(3, function.cherries)
3838

3939
def test_setting_attributes_on_a_bound_method_directly(self):
4040
obj = Class()
41-
with self.assertRaises(AttributeError): obj.method.cherries = 3
42-
41+
with self.assertRaises(AttributeError): obj.method.cherries = 3
42+
4343
def test_setting_attributes_on_methods_by_accessing_the_inner_function(self):
4444
obj = Class()
4545
obj.method.__func__.cherries = 3
4646
self.assertEqual(3, obj.method.cherries)
47-
47+
4848
def test_functions_can_have_inner_functions(self):
4949
function2.get_fruit = function
5050
self.assertEqual('pineapple', function2.get_fruit())
@@ -60,7 +60,7 @@ def __get__(self, obj, cls):
6060
return (self, obj, cls)
6161

6262
binding = BoundClass()
63-
63+
6464
def test_get_descriptor_resolves_attribute_binding(self):
6565
bound_obj, binding_owner, owner_type = self.binding
6666
# Look at BoundClass.__get__():
@@ -82,9 +82,9 @@ def __set__(self, obj, val):
8282
self.choice = val
8383

8484
color = SuperColor()
85-
85+
8686
def test_set_descriptor_changes_behavior_of_attribute_assignment_changes(self):
8787
self.assertEqual(None, self.color.choice) #####
8888
self.color = 'purple'
8989
self.assertEqual('purple', self.color.choice)
90-
90+

python 3/koans/about_methods.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def my_global_function(a,b):
1313
class AboutMethods(Koan):
1414
def test_calling_a_global_function(self):
1515
self.assertEqual(5, my_global_function(2,3))
16-
16+
1717
# NOTE: Wrong number of arguments is not a SYNTAX error, but a
1818
# runtime error.
1919
def test_calling_functions_with_wrong_number_of_arguments(self):
@@ -22,26 +22,26 @@ def test_calling_functions_with_wrong_number_of_arguments(self):
2222
except TypeError as exception:
2323
msg = exception.args[0]
2424

25-
self.assertRegexpMatches(msg, r'my_global_function\(\) takes exactly 2 positional arguments \(0 given\)') #####
26-
25+
self.assertRegexpMatches(msg, r'my_global_function\(\) missing 2 required positional arguments') #####
26+
2727
try:
2828
my_global_function(1, 2, 3)
2929
except Exception as e:
3030
msg = e.args[0]
31-
31+
3232
# Note, watch out for parenthesis. They need slashes in front!
33-
self.assertRegexpMatches(msg, r'my_global_function\(\) takes exactly 2 positional arguments')
34-
33+
self.assertRegexpMatches(msg, r'my_global_function\(\) takes 2 positional arguments')
34+
3535
# ------------------------------------------------------------------
36-
36+
3737
def pointless_method(self, a, b):
3838
sum = a + b
3939

4040
def test_which_does_not_return_anything(self):
4141
self.assertEqual(None, self.pointless_method(1, 2))
4242
# Notice that methods accessed from class scope do not require
4343
# you to pass the first "self" argument?
44-
44+
4545
# ------------------------------------------------------------------
4646

4747
def method_with_defaults(self, a, b='default_value'):
@@ -65,13 +65,13 @@ def test_calling_with_variable_arguments(self):
6565

6666
def function_with_the_same_name(self, a, b):
6767
return a + b
68-
68+
6969
def test_functions_without_self_arg_are_global_functions(self):
7070
def function_with_the_same_name(a, b):
7171
return a * b
7272

7373
self.assertEqual(12, function_with_the_same_name(3,4))
74-
74+
7575
def test_calling_methods_in_same_class_with_explicit_receiver(self):
7676
def function_with_the_same_name(a, b):
7777
return a * b
@@ -82,9 +82,9 @@ def function_with_the_same_name(a, b):
8282

8383
def another_method_with_the_same_name(self):
8484
return 10
85-
85+
8686
link_to_overlapped_method = another_method_with_the_same_name
87-
87+
8888
def another_method_with_the_same_name(self):
8989
return 42
9090

@@ -113,7 +113,7 @@ def test_pass_does_nothing_at_all(self):
113113
# ------------------------------------------------------------------
114114

115115
def one_line_method(self): return 'Madagascar'
116-
116+
117117
def test_no_indentation_required_for_one_line_statement_bodies(self):
118118
self.assertEqual('Madagascar', self.one_line_method())
119119

@@ -122,16 +122,16 @@ def test_no_indentation_required_for_one_line_statement_bodies(self):
122122
def method_with_documentation(self):
123123
"A string placed at the beginning of a funtion is used for documentation"
124124
return "ok"
125-
125+
126126
def test_the_documentation_can_be_viewed_with_the_doc_method(self):
127127
self.assertRegexpMatches(self.method_with_documentation.__doc__, 'A string placed at the beginning of a funtion is used for documentation')
128128

129129
# ------------------------------------------------------------------
130130

131-
class Dog:
131+
class Dog:
132132
def name(self):
133133
return "Fido"
134-
134+
135135
def _tail(self):
136136
# Prefixing a method with an underscore implies private scope
137137
return "wagging"
@@ -142,7 +142,7 @@ def __password(self):
142142
def test_calling_methods_in_other_objects_require_explicit_receiver(self):
143143
rover = self.Dog()
144144
self.assertEqual('Fido', rover.name())
145-
145+
146146
def test_private_access_is_implied_but_not_enforced(self):
147147
rover = self.Dog()
148148

@@ -151,11 +151,11 @@ def test_private_access_is_implied_but_not_enforced(self):
151151

152152
def test_attributes_with_double_underscore_prefixes_are_subject_to_name_mangling(self):
153153
rover = self.Dog()
154-
with self.assertRaises(NameError): password = __password()
155-
154+
with self.assertRaises(NameError): password = __password()
155+
156156
# But this still is!
157157
self.assertEqual('password', rover._Dog__password())
158-
158+
159159
# Name mangling exists to avoid name clash issues when subclassing.
160160
# It is not for providing effective access protection
161161

0 commit comments

Comments
 (0)