-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path37_Data_abstraction.py
More file actions
57 lines (41 loc) · 1.27 KB
/
37_Data_abstraction.py
File metadata and controls
57 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Data Abstraction -> Python abstraction is used to hide the implementation details from the user and expose only necessary parts,
making the code simpler and easier to interact with."""
# Abstract Base class
from abc import ABC, abstractmethod
class Greet(ABC): # abstract class
@abstractmethod
def say_hello(self):
pass
class English(Greet):
def say_hello(self):
return "Hello!"
g = English()
print(g.say_hello())
# Abstractmethod
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass # Abstract method, no implementation here
# Concrete method
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass # Abstract method, no implementation here
def move(self):
return "Mving" # Concrete method with implementation
# Abstract Properties
from abc import ABC, abstractmethod
class Animal(ABC):
@property
@abstractmethod
def species(self):
pass # Abstract property, must be implemented by subclasses
class Dog(Animal):
@property
def species(self):
return "Canine"
# Instantiate the concrete subclass
dog = Dog()
print(dog.species)