Browse Source

teil 26 lambda Funktionen.

develop
Olli Graf 5 months ago
parent
commit
3607e2ef69
  1. 17
      gettattr/name.py
  2. 28
      gettattr/testname.py
  3. 7
      teil26/square.py
  4. 5
      teil26/square_lambda.py

17
gettattr/name.py

@ -0,0 +1,17 @@
class Name:
def __init__(self,vorname,name):
self.vorname = vorname
self.name = name
def __getattr__(self,attr):
if attr == 'fullname':
return self.vorname + ' ' + self.name
elif attr == 'sortname':
return self.name + ',' + self.vorname
else:
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{attr}'")

28
gettattr/testname.py

@ -0,0 +1,28 @@
import unittest
import logging
from name import Name
__LOGLEVEL__ = logging.DEBUG
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=__LOGLEVEL__)
class TestName(unittest.TestCase):
def setUp(self):
logging.debug('setting up test')
self.name = Name('Homer', 'Simpson')
def test_getattr(self):
fullname = self.name.fullname
sortname = self.name.sortname
self.assertEqual('Homer Simpson', fullname)
self.assertEqual('Simpson,Homer', sortname)
if __name__ == '__main__':
logging.debug('Test startet.')
unittest.main()

7
teil26/square.py

@ -0,0 +1,7 @@
def square(x):
return x** 2
sq5 = square(5)
print(f'{sq5}')

5
teil26/square_lambda.py

@ -0,0 +1,5 @@
square = lambda x: x** 2
print(f'{square(5)}')
Loading…
Cancel
Save