I'm having problems with a program. It's a program using a data class to print out movie characters. However I don't have a lot of practice with data classes and don't really know how to use them. So I'm wondering if someone could help me solve this problem?
This is the main
import Character
# Creates a character called Indiana
Indiana = Character.Character('Indiana', 'Human', 'Earth')
print(Indiana.to_string())
# Creates an empty character later filled with Luke Skywalker
luke = Character.Character()
luke.set_name('Luke Skywalker')
luke.set_kind('Human')
luke.set_planet('Tatooine')
print(luke.to_string())
I haven't really gotten too far with the data class, so far I've only come up with this
from dataclasses import dataclass
@dataclass
class Character:
name: str
kind: str
planet: str
def char(name,kind,planet):
set_name = name
set_kind = kind
set_planet = planet
and the output should be like this
Output:
Indiana is a(n) Human from Earth
Luke Skywalker is a(n) Human from Tatooine
I hope someone can help me!
CodePudding user response:
Dataclasses automatically create constructors, setters, getters, and string conversion methods as appropriate to the options on the @dataclass
decorator and the class attributes you define in the body of the class. Delete your char
function and do:
from Character import Character
# Creates a character called Indiana
indiana = Character('Indiana', 'Human', 'Earth')
print(indiana)
# Creates an empty character later filled with Luke Skywalker
luke = Character('', '', '')
luke.name = 'Luke Skywalker'
luke.kind = 'Human'
luke.planet = 'Tatooine'
print(luke)
This prints:
Character(name='Indiana', kind='Human', planet='Earth')
Character(name='Luke Skywalker', kind='Human', planet='Tatooine')
If you want your Character
's fields to have default values, set them in the Character
class like this:
@dataclass
class Character:
name: str = ''
kind: str = ''
planet: str = ''
and now you can do luke = Character()
and it will fill in the three ''
empty strings for you automatically.
If you want to change the way that a Character
is converted to a string, add a __str__
method, e.g.:
@dataclass
class Character:
name: str = ''
kind: str = ''
planet: str = ''
def __str__(self) -> str:
return f"{self.name}, a {self.kind} from {self.planet}"
Note that in Python you don't have to explicitly call a method like to_string
to print an object as a string; the print
function will automatically convert its arguments to strings with the str()
function, which in turn will call the __str__
method of the object to do the conversion.
CodePudding user response:
You should be getting an error when you try to instantiate empty character.. You can avoid that by specifying default value .. To print, you can override str method see below
from dataclasses import dataclass
@dataclass
class Character:
name:str=None
kind:str=None
planet:str=None
def set_char(self,name,kind,planet):
self.name=name
self.kind=kind
self.planet=planet
def __str__(self):
return f'{self.name} is a(n) {self.kind} from {self.planet}'
Indiana=Character('Indiana','Human','Earth') print(Indiana) Luke=Character() Luke.set_char('Luke SkyWalker','Human','Tatooine') print(Luke)