I'm new to Python and am trying to understand how classes work. I've written a simple class, but I'm having trouble with initializing attributes and using them correctly. Here’s what I have so far:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")
I want to understand:
- Is this the correct way to initialize attributes in a class? Are there other best practices I should know about?
- How do I create instances of this class? For example, if I want to create two people, John and Jane, how would I do that?
- How can I access and modify attributes after creating an instance? If I want to change someone's name or age, how should I do it?
I created an instance of the Person class like this:
john = Person("John", 30)
This seems to work, and I can call john.introduce()
to get the output I expect. However, I'm not sure if this is the best approach to initialize and access attributes. When I try to change the age attribute directly, like this:
john.age = 31
I want to understand if:
- There's a better way to set or update attributes, like with specific methods or property decorators.
- I'm following the best practices for initializing attributes and creating instances.
- There are any recommended practices for modifying attribute values after instance creation, especially for larger projects.
I'm new to Python and am trying to understand how classes work. I've written a simple class, but I'm having trouble with initializing attributes and using them correctly. Here’s what I have so far:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")
I want to understand:
- Is this the correct way to initialize attributes in a class? Are there other best practices I should know about?
- How do I create instances of this class? For example, if I want to create two people, John and Jane, how would I do that?
- How can I access and modify attributes after creating an instance? If I want to change someone's name or age, how should I do it?
I created an instance of the Person class like this:
john = Person("John", 30)
This seems to work, and I can call john.introduce()
to get the output I expect. However, I'm not sure if this is the best approach to initialize and access attributes. When I try to change the age attribute directly, like this:
john.age = 31
I want to understand if:
- There's a better way to set or update attributes, like with specific methods or property decorators.
- I'm following the best practices for initializing attributes and creating instances.
- There are any recommended practices for modifying attribute values after instance creation, especially for larger projects.
1 Answer
Reset to default -3You're off to a good start, but there are some issues and areas for improvement in your code and understanding. The correct way of writing is:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")