Reputation: 7
I'm trying to see if it's possible to create a loop in a child class with for loop
?
Anyway, here's what I made and trying to figure out how to achieve this (line 22-23) but unfortunately can't find a way. I'm new to python btw and exploring things
class Valkyrie:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Character(Valkyrie):
def __init__(self, fname, lname, age):
super().__init__(fname, lname)
self.edad = age
def intro(self):
print(self.firstname, self.lastname, self.edad)
def introd(self):
print(self.firstname, self.lastname,",", self.edad)
kk = Character("Kiana", "Kaslana", 16)
for x in kk:
print(x)
rm = Character("Mei", "Raiden", 18)
bz = Character("Bronya", "Zaychick", 14)
kk.intro()
kk.introd()
rm.intro()
bz.intro()
Upvotes: -1
Views: 84
Reputation: 69
I don't understand your example but summarizing, yes, you can create a loop inside a child class.
Let's consider this example:
class Parent_class:
def __init_(self,name,last_name,age):
self.name = name
self.last_name = last_name
selg.age = age
def print_name(self):
print('The name is ',self.name,' ',self.last_name,'\n')
class Child_class(list(PARENT_CLASSES)):
def __init__(self, name, last_name, age):
super().__init__(name, last_name, age)
def introduce(self):
for person in PARENT_CLASSES:
person.print_name()
If you pass a list of PARENT_CLASSES
to the child class and, then, invoke its introduce
function, it will give you the details of every person listed in PARENT_CLASSES
Upvotes: 1