From e675f7593d330608c4cf31946a6706227f106e6e Mon Sep 17 00:00:00 2001 From: Emma Nardino Date: Thu, 12 Sep 2024 10:11:43 +0200 Subject: [PATCH] delete python --- TP01/python/objects.py | 26 ------------------- TP01/python/type_unions.py | 53 -------------------------------------- TP01/python/typecheck.py | 16 ------------ 3 files changed, 95 deletions(-) delete mode 100644 TP01/python/objects.py delete mode 100644 TP01/python/type_unions.py delete mode 100644 TP01/python/typecheck.py diff --git a/TP01/python/objects.py b/TP01/python/objects.py deleted file mode 100644 index db07c07..0000000 --- a/TP01/python/objects.py +++ /dev/null @@ -1,26 +0,0 @@ -# Class without base class -class Base(): - # Constructor (called by "Base()" when creating an object) - def __init__(self): - self.a = "set in Base's constructor" - - def f(self): - return self.a - - def g(self): - return "returned from Base's g()" - - -class Derived(Base): - # Function with the same name override the base class function. - def f(self): - return "returned from Derived's f()" - - -b = Base() # Create an object of Base -print(b.f()) # Method call, as usual OO languages -print(b.g()) - -d = Derived() -print(d.f()) -print(d.g()) diff --git a/TP01/python/type_unions.py b/TP01/python/type_unions.py deleted file mode 100644 index be5bd60..0000000 --- a/TP01/python/type_unions.py +++ /dev/null @@ -1,53 +0,0 @@ -from typing import List - -# int | float means ``either an int or a float''. -NUMBER = int | float # or Union[int, float] with Union imported from typing - - -def add_numbers(a: NUMBER, b: NUMBER) -> NUMBER: - return a + b - - -# Both int and floats can be passed to the function -print(add_numbers(1, 4.3)) - - -def divide_numbers(a: NUMBER, b: NUMBER) -> float: - return a / b - - -print(divide_numbers(1, 2)) - -# Declare the type of a list whose elements are numbers. -LIST_OF_NUMBERS = List[NUMBER] - - -def increment(a: LIST_OF_NUMBERS) -> LIST_OF_NUMBERS: - return [x + 1 for x in a] - - -print(increment([1, 2, 3])) - -# Skip the end if you are late. - -# The type DEEP_LIST_OF_NUMBERS is a special case since it references itself. -# The identifier DEEP_LIST_OF_NUMBERS cannot be used before the end of its -# initialization, but the circular dependency can be broken using the string -# 'DEEP_LIST_OF_NUMBERS' instead. -DEEP_LIST_OF_NUMBERS = NUMBER | List['DEEP_LIST_OF_NUMBERS'] - - -def deep_increment(d: DEEP_LIST_OF_NUMBERS) -> DEEP_LIST_OF_NUMBERS: - if isinstance(d, list): - # Note the unusual typing rule applied by Pyright here: because we are - # in the 'isinstance(d, list)' branch, it knows that d is a list, - # and accepts to iterate over it. - return [deep_increment(e) for e in d] - else: - # ... and here, in the 'else' branch Pyright knows that d is - # not a list, - # and can deduce that it is a NUMBER. - return d + 1 - - -print(deep_increment([1, [2, 3]])) diff --git a/TP01/python/typecheck.py b/TP01/python/typecheck.py deleted file mode 100644 index 58f8288..0000000 --- a/TP01/python/typecheck.py +++ /dev/null @@ -1,16 +0,0 @@ -# Typing annotations for variables: -# name: type -int_variable: int -float_variable: float -int_variable = 4.2 # Static typing error, but no runtime error -float_variable = 42.0 # OK -float_variable = int_variable # OK - - -# Typing annotations for functions (-> means "returns") -def int_to_string(i: int) -> str: - return str(i) - - -print(int_to_string('Hello')) # Static typing error, but no runtime error -print(int_to_string(42) / 5) # Both static and runtime error