class Text:
pass
# Creating an instance of the Text class
my_text = Text()
print(my_text) # Output: <__main__.Text object at 0x...>
<__main__.Text object at 0x108bd43e0>
class
keyword followed by the class name (CamelCase convention).Text
and created an instance stored in my_text
.__init__()
method to initialize attributes during object creation.class Text:
def __init__(self, content):
self.content = content # Instance attribute
def word_count(self): # Method
return len(self.content.split())
def shout(self): # Another method
result = self.content.upper()
result = result.replace(".", "!")
return result
my_text = Text("Hello, World! This is a test.")
print(my_text.word_count()) # Output: 6
print(my_text.shout()) # Output: HELLO, WORLD! THIS IS A TEST!
6
HELLO, WORLD! THIS IS A TEST!
__init__()
and belong to a specific instance.Seminar: LLM, WiSe 2024/25