diff --git a/.DS_Store b/.DS_Store index fba7091..b0e2677 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Pass2/Inheritance.py b/Pass2/Inheritance.py new file mode 100755 index 0000000..7456678 --- /dev/null +++ b/Pass2/Inheritance.py @@ -0,0 +1,23 @@ +class Person: + + def __init__(self, first, last): + self.firstname = first + self.lastname = last + + def Name(self): + return self.firstname + " " + self.lastname + +class Employee(Person): + + def __init__(self, first, last, staffnum): + Person.__init__(self,first, last) + self.staffnumber = staffnum + + def GetEmployee(self): + return self.Name() + ", " + self.staffnumber + +x = Person("Marge", "Simpson") +y = Employee("Homer", "Simpson", "1337") + +print(x.Name()) +print(y.GetEmployee()) \ No newline at end of file diff --git a/Pass2/OOP basics.py b/Pass2/OOP basics.py new file mode 100755 index 0000000..6587f6c --- /dev/null +++ b/Pass2/OOP basics.py @@ -0,0 +1,12 @@ +class MyClass: + #My first class + i = 12345 + str = "Something" + + def myFunc(self): + return 'hello world' + +x = MyClass() +print(x.i) +print(x.str) +print(x.myFunc()) diff --git a/Pass2/OOP with constructor.py b/Pass2/OOP with constructor.py new file mode 100755 index 0000000..622d75c --- /dev/null +++ b/Pass2/OOP with constructor.py @@ -0,0 +1,28 @@ +class MyMath: + myInt = 12345 #class variable the same for all instances + """“Private” instance variables that cannot be accessed except from + inside an object don’t exist in Python. However, there is a + convention that is followed by most Python code: a name prefixed + with an underscore (e.g. _spam) should be treated as a non-public + part of the API (whether it is a function, a method or a data member). + It should be considered an implementation detail and subject to change without notice. Since + there is a valid use-case for class-private members (namely to avoid name clashes of names + with names defined by subclasses), there is limited support for such a mechanism, + called name mangling. Any identifier of the form __spam (at least two leading + underscores, at most one trailing underscore) is textually replaced with + _classname__spam, where classname is the current class name with leading + underscore(s) stripped. This mangling is done without regard to the syntactic + position of the identifier, as long as it occurs within the definition of a class.""" + + __privateString = "Behandla som privat och blir manglad :)" + + def __init__(self, x, y): + self.x = x #instance variables + self.y = y + print(self.__privateString) + +test1 = MyMath(3.14,2.718) +test2 = MyMath(42,1337) +print(test1.x,test1.y,test1.myInt) +print(test2.x,test2.y,test2.myInt) +#print(test2.__privateString) \ No newline at end of file diff --git a/Pass2/scope.py b/Pass2/scope.py new file mode 100755 index 0000000..3c93c74 --- /dev/null +++ b/Pass2/scope.py @@ -0,0 +1,22 @@ +def scope_test(): + def do_local(): + spam = "local spam" + + def do_nonlocal(): + nonlocal spam + spam = "nonlocal spam" + + def do_global(): + global spam + spam = "global spam" + + spam = "test spam" + do_local() + print("After local assignment:", spam) + do_nonlocal() + print("After nonlocal assignment:", spam) + do_global() + print("After global assignment:", spam) + +scope_test() +print("In global scope:", spam) \ No newline at end of file diff --git a/Pass2/struct.py b/Pass2/struct.py new file mode 100755 index 0000000..fbdefbc --- /dev/null +++ b/Pass2/struct.py @@ -0,0 +1,11 @@ +class Car: + pass + +myCar = Car() # Create an empty employee record + +# Fill the fields of the record +myCar.model = 'V 70' +myCar.brand = 'Volvo' +myCar.year = 2005 + +print(myCar.model) \ No newline at end of file