使用用户输入访问类实例的属性 [英] Accessing a class instance's attributes using user input

查看:81
本文介绍了使用用户输入访问类实例的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码:

class vehicle(object):
    def __init__(self):
        self.age = 6
        self.make = 8
        self.colour = 'colour'
        self.cost = 'cost'

class car(vehicle):
    def __init__(self):
        vehicle.__init__(self)
        self.type = 'car'

car1 = car()
print car1.make, car1.colour, car1.cost, car1.type, car1.age

n = raw_input()
dic = {'name' : n}
dic['name'] = car()
print dic['name'].make

最后,我能够解决之前遇到的一个问题:创建一个汽车类的实例,其名称为用户在n中输入的名称

In the last bit, I was able to resolve a previous issue I had: Creating an instance of the car class with its name being one that the user inputs in n

现在,说我想让用户输入一个名称,现在我必须找到具有该名称的类的实例.

Now, say I wanna ask the user to input a name and now I have to find the class' instance that has that name.

例如,如果某个时刻创建了一个名称为car2的实例.现在,用户希望获取有关car2的信息并输入"car2".如何使用此输入访问名为car2的实例的属性?

For example if at one point an instance with the name car2 was created. Now user wants to get info about car2 and inputs 'car2'. how can I use this input to access attributes of the instance called car2?

我尝试过:

a = raw_input()
dic['fetch'] = a
dic['fetch'].make

不起作用.

推荐答案

在我看来,您有点误会.您将输入分配给字典的方式没有意义.您的描述表明您需要一个将名称"映射到汽车描述的字典.

It seems to me you have a fair bit of misunderstanding. The way you're assigning the input into the dictionary doesn't make sense. Your description indicates that you want a dictionary that maps a "name" to a car description.

您最初创建的字典已关闭.您当前的操作方式实际上是在分配汽车数据时丢失了用户输入的名称.一些更好的变量命名可能会对您有所帮助.像这样创建字典:

Your initial creation of the dictionary is off. The way you're currently doing it, you're actually losing the name the user inputs when you assign the car data. Some better variable naming might help you. Create your dictionary like this:

cars_by_name = dict()
name = raw_input()
cars_by_name[name] = car()

因此,您现在有了一个名称(由用户提供),该名称可以映射到汽车说明.

So now you have a name (given by the user) that maps to a car description.

现在,您需要再次获取相同的car实例.您可以通过使用名称作为字典中的键来完成此操作:

Now you need to fetch that same car instance again. You do it by using the name as the key in the dictionary:

name2 = raw_input()
print cars_by_name[name2].make

接下来,让我们看一下您的课程.我的第一个问题:为什么需要vehiclecar类?如果除了car之外再也没有要从vehicle继承的类,那么您实际上并不需要它们.即使您确实计划有更多的子类,我仍可能会在这里建议不要继承.您的vehicle没有子类继承的行为(方法).它只是一个数据对象.使用鸭子输入在Python中受到强烈鼓励,它继承自没有方法的类不会给你买任何东西. (基类可以使用方法为您提供的好处是,您只需要在一个地方定义该方法,这样以后就可以更轻松地进行修改.)特别是在您的情况下,似乎没有任何动机去创建完全是一个子类.适用于所有车辆的单一班级就可以正常工作.因此,让我们简化您的课程:

Next, let's look at your classes. My first question: why do you need a vehicle and a car class? If you're never going to have classes other than car inheriting from vehicle, you don't really need them both. Even if you do plan the have more subclasses, I would probably still recommend against inheritance here. Your vehicle has no behavior (methods) for subclasses to inherit. It's just a data object. With duck typing so strongly encouraged in Python, inheriting from a class with no methods doesn't buy you anything. (What a base class would buy you with methods is that you'd only have to define the method in one place, making it easier to modify later on.) Particularly in your case, there doesn't seem to be any motivation to create a subclass at all. A single class for all vehicles will work just fine. So let's simplify your classes:

class Vehicle(object):
    def __init__(self):
        self.age = 6
        self.make = 8
        self.colour = 'colour'
        self.cost = 'cost'
        self.type = 'car'

(还要注意,类名通常在Python中以驼峰形式给出.)现在这里还有一个问题:这些常量.并非所有的Vehicle都将具有相同的值.实际上,大多数不会.因此,让它们成为初始化程序的参数:

(Also, note that class names are usually given in camel case in Python.) Now there's one more problem here: those constants. Not all Vehicles are going to have those same values; in fact, most won't. So lets make them arguments to the initializer:

class Vehicle(object):
    def __init__(self, age, make, colour, cost, type):
        self.age = age
        self.make = make
        self.colour = colour
        self.cost = cost
        self.type = type

然后您创建一个像这样的人:

Then you create one like this:

v = Vehicle(6, 8, 'colour', 'cost', 'car')

祝您学习愉快.希望这会有所帮助.

Good luck in your endeavors learning. Hope this helps.

这篇关于使用用户输入访问类实例的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆