How to take the string from input of argparse and use it as an object

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to take the string from input of argparse and use it as an object



I am trying to create a program that reads a string off an argparse input and uses that string to call a certain object and use that object for the rest of the function.


#!/usr/bin/python
import argparse

class car:
def __init__(self, color, year):
self.color = color
self.year = year

Beatle = car("blue", 1973)

parser = argparse.ArgumentParser()
parser.add_argument("--vehicleType")
args = parser.parse_args()

print("This "+args.vehicleType+ " is nice")
print("It was made:")
print(2018-args.vehicleType.year)
print("years ago")



However, I keep getting back this error:


Traceback (most recent call last):
File "./test.py", line 17, in <module>
print(2018-args.vehicleType.year)
AttributeError: 'str' object has no attribute 'year'



I think the program is reading args.vehicleType as a string, while I want to read it in as an object. How do I get around this?
-Cheers, and thanks in advance! ~O. Fried





Yes, args.vehicleType is a string. It's the responsibility of your own code to map strings like that on to objects like Beatle. The variable name Beatle references a car object. The string 'Beattle' does not reference that.
– hpaulj
10 mins ago


args.vehicleType


Beatle


Beatle


car




1 Answer
1



Would it be possible to actually create the object after? Something along these lines:


#!/usr/bin/python
import argparse
class car:
def __init__(self, color, year, type):
self.color = color
self.year = year
self.type = type

parser = argparse.ArgumentParser()
parser.add_argument("--vehicleType") # Accept the argument as vehicle type.
args = parser.parse_args()

Beatle = car("blue", 1973, str(args.vehicleType)) # Now create the object.

print("This " + Beatle.type+ " is nice") # Use it here.
print("It was made:")
print(2018-Beatle.year) # And here.
print("years ago.")





yes this seems useful, thanks! can you just explain what dest is though? I really dont understand...
– O. Fried
9 mins ago





wait I don't think so. Because if I added another instance of the class vehicle I would need to change the data inside it-meaning it couldnt be "blue" and 1973 for all of them
– O. Fried
5 mins ago





I'm sorry, you actually do not need dest. FYI: docs.python.org/3.6/library/argparse.html#dest
– Vivek
5 mins ago





wait I don't think so. Because if I added another instance of the class vehicle I would need to change the data inside it-meaning it couldnt be "blue" and 1973 for all of them
– O. Fried
4 mins ago





I'm sorry but I did not understand that comment. What are you particularly trying to achieve?
– Vivek
2 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions