Maven_super_31_100_Python_51_65 - itnett/FTD02H-N GitHub Wiki

Her er en videreføring av listen med de neste 15 Python-konseptene, fra og med nummer 51. Hvert konsept har en forklaring, et kodeeksempel, og lenker til ressurser som vil hjelpe deg å forstå emnet bedre.

51. Instance Methods

class MyClass:
    def __init__(self, name):
        self.name = name
    def greet(self):
        return f"Hello, {self.name}"
obj = MyClass("John")
print(obj.greet())

52. Class Methods

class MyClass:
    @classmethod
    def class_method(cls):
        return "This is a class method"
print(MyClass.class_method())

53. Static Methods

class MyClass:
    @staticmethod
    def static_method():
        return "This is a static method"
print(MyClass.static_method())

54. Inheritance

class Animal:
    def __init__(self, name):
        self.name = name
    def speak(self):
        return f"{self.name} makes a noise"
class Dog(Animal):
    def speak(self):
        return f"{self.name} barks"
dog = Dog("Fido")
print(dog.speak())

55. Polymorphism

class Animal:
    def speak(self):
        pass
class Dog(Animal):
    def speak(self):
        return "Bark"
class Cat(Animal):
    def speak(self):
        return "Meow"
for animal in (Dog(), Cat()):
    print(animal.speak())

56. Encapsulation

class MyClass:
    def __init__(self, name):
        self.__name = name
    def get_name(self):
        return self.__name
obj = MyClass("John")
print(obj.get_name())

57. Abstraction

from abc import ABC, abstractmethod
class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass
class Dog(Animal):
    def sound(self):
        return "Bark"
dog = Dog()
print(dog.sound())

58. File Handling - Reading

with open('file.txt', 'r') as file:
    content = file.read()
    print(content)

59. File Handling - Writing

with open('file.txt', 'w') as file:
    file.write("Hello, World!")

60. File Handling - Appending

with open('file.txt', 'a') as file:
    file.write("\nNew line")

61. Error Handling - Try/Except

try:
    print(10 / 0)
except ZeroDivisionError:
    print("You cannot divide by zero!")

62. Error Handling - Finally

try:
    print(10 / 0)
except ZeroDivisionError:
    print("You cannot divide by zero!")
finally:
    print("This block is always executed.")

63. Error Handling - Else

try:
    result = 10 / 2
except ZeroDivisionError:
    print("You cannot divide by zero!")
else:
    print("No errors occurred.")

64. Custom Exceptions

class CustomError(Exception):
    pass
try:
    raise CustomError("This is a custom error")
except CustomError as e:
    print(e)

65. Logging

import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("This

Her er en fortsettelse av de neste 15 Python-konseptene, fra og med nummer 51 til 65. Hver kommando eller funksjon er forklart, og det er gitt eksempler og lenker til ressurser som kan hjelpe deg med å forstå hvert tema i dybden.

### 51. **Instance Methods**
```python
class MyClass:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hello, {self.name}"

obj = MyClass("John")
print(obj.greet())

52. Class Methods

class MyClass:
    @classmethod
    def class_method(cls):
        return "This is a class method"

print(MyClass.class_method())

53. Static Methods

class MyClass:
    @staticmethod
    def static_method():
        return "This is a static method"

print(MyClass.static_method())

54. Inheritance

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} makes a noise"

class Dog(Animal):
    def speak(self):
        return f"{self.name} barks"

dog = Dog("Fido")
print(dog.speak())

55. Polymorphism

class Animal:
    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return "Bark"

class Cat(Animal):
    def speak(self):
        return "Meow"

for animal in (Dog(), Cat()):
    print(animal.speak())

56. Encapsulation

class MyClass:
    def __init__(self, name):
        self.__name = name

    def get_name(self):
        return self.__name

obj = MyClass("John")
print(obj.get_name())

57. Abstraction

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Bark"

dog = Dog()
print(dog.sound())
  • Forklaring: Abstraksjon er prosessen med å skjule detaljer og vise bare essensielle funksjoner. Dette oppnås i Python ved å bruke abstrakte klasser som ikke kan instansieres og som krever at subklasser implementerer bestemte metoder.
  • Ressurser:

58. File Handling - Reading

with open('file.txt', 'r') as file:
    content = file.read()
    print(content)

59. File Handling - Writing

with open('file.txt', 'w') as file:
    file.write("Hello, World!")

60. File Handling - Appending

with open('file.txt', 'a') as file:
    file.write("\nNew line")

61. Error Handling - Try/Except

try:
    print(10 / 0)
except ZeroDivisionError:
    print("You cannot divide by zero!")

62. Error Handling - Finally

try:
    print(10 / 0)
except ZeroDivisionError:
    print("You cannot divide by zero!")
finally:
    print("This block is always executed.")

63. Error Handling - Else

try:
    result = 10 / 2
except ZeroDivisionError:
    print("You cannot divide by zero!")
else: