Home > Blockchain >  Python call a list from outside the method and class then return what I expect
Python call a list from outside the method and class then return what I expect

Time:01-20

I want to print at the end of code, something like this: (car1, car2)

But the loop I created at the end, can't find the the new_list because it is inside the class Vehicles.

class Vehicles:
    color = ''
    wheels = 0
    
    def __init__(self, color, wheels):
        self.color = color
        self.wheels = wheels
    
    def filtering(self, list_vehicles, vehicle):
        new_list = []
        new_list = list(filter(lambda x: True if x.__class__.__name__ == vehicle else False, list_vehicles))
        return (new_list)
    
    def __str__(self):
        print(f"This bus is {self.color} and has {self.wheels} wheels.")
    
bus1 = Vehicles("white", 6)
bus1.__str__()

class Car(Vehicles):
    speed = 0
    changes = 0
    
    def __init__(self, color, wheels, speed, changes):
        super().__init__(color, wheels)
        self.speed = speed
        self.changes = changes
    
    def __str__(self):
        print(f"This car is {self.color}, has {self.wheels} wheels, his speed is {self.speed} mph and it has {self.changes} changes.")

car1 = Car("red", 4, 110, 5)
car1.__str__()
car2 = Car("blue", 4, 130, 6)
car2.__str__()

filtering([bus1, car1, car2], "Car")

for x in new_list:
    print(x)

CodePudding user response:

Basically you have to use a class object to call a class function or you have to use a static method instead.One more thing you are returning a list from your function and not saving it anywhere. You have to make a new variable named new_list to save that, so that you can traverse over it. So if you make this change, it will run fine.

new_list  = car1.filtering([bus1, car1, car2], "Car")

CodePudding user response:

You are using an instance method and yet you don't provide it with the access to the instance object.

Here would be one way of doing it.

class Vehicles:
    color = ''
    wheels = 0
    
    def __init__(self, color, wheels):
        self.color = color
        self.wheels = wheels
    
    def filtering(self, list_vehicles, vehicle):
        new_list = []
        new_list = list(filter(lambda x: True if x.__class__.__name__ == vehicle else False, list_vehicles))
        return new_list
    
    def __str__(self):
        return f"This bus is {self.color} and has {self.wheels} wheels."
    
bus1 = Vehicles("white", 6)
bus1.__str__()

class Car(Vehicles):
    speed = 0
    changes = 0
    
    def __init__(self, color, wheels, speed, changes):
        super().__init__(color, wheels)
        self.speed = speed
        self.changes = changes
    
    def __str__(self):
        return f"This car is {self.color}, has {self.wheels} wheels, his speed is {self.speed} mph and it has {self.changes} changes."

car1 = Car("red", 4, 110, 5)
car1.__str__()
car2 = Car("blue", 4, 130, 6)
car2.__str__()

new_list  = car1.filtering([bus1, car1, car2], "Car")

for x in new_list:
  print(x)

I have fixed some other issues with the code as well. One of them being, not returning anything from __str__ (dunder str method). You should return the string representation actually. As you didn't return anything, python just got the implicit return of None.

Edit:

That instance method is not even using anything instance specific. So you can simply make it a static method or class method. For brevity we show the example with class method. As mentioned by @chepner) this solution would be better than passing the string and using __class__ is to use the isinstance() like this. Notice that here we have made the method a class method. As a result, the first parameter denotes or object denoting the class itself.

class Vehicles:
    color = ''
    wheels = 0
    
    def __init__(self, color, wheels):
        self.color = color
        self.wheels = wheels
    @classmethod
    def filtering(cls,  list_vehicles):
        new_list = []
        new_list = list(filter(lambda x: True if isinstance(x,cls) else False, list_vehicles))
        return new_list
    
    def __str__(self):
        return f"This bus is {self.color} and has {self.wheels} wheels."
    
bus1 = Vehicles("white", 6)
bus1.__str__()

class Car(Vehicles):
    speed = 0
    changes = 0
    
    def __init__(self, color, wheels, speed, changes):
        super().__init__(color, wheels)
        self.speed = speed
        self.changes = changes
    
    def __str__(self):
        return f"This car is {self.color}, has {self.wheels} wheels, his speed is {self.speed} mph and it has {self.changes} changes."

car1 = Car("red", 4, 110, 5)
car1.__str__()
car2 = Car("blue", 4, 130, 6)
car2.__str__()

new_list  = car1.filtering([bus1, car1, car2])

for x in new_list:
  print(x,",")
  • Related