python 3 - instantiating class from user input

B

Brad Bailey

I dont understand why this is such a big deal. Nor do i understand why google can't find a reasonable answer. If one can't figure out from the title what I'm trying to do, then a look at code should firmly plant the intent. The general idea of the code is, in my opinion, very basic.

I notice, that when you are coding a class instance, it's in the format of:
newInstance = someClass()

OK, so we are calling someClass as if it's a function. Is there a way to do something to the effect of:
someClass(newInstance)

I've seen all kinds of hacks and workarounds that seem 10 times more complicated that this ought to be.

class inventoryItem:
itsDecrtiption = None
itsVendor = None
itsCost = None
itsMarkup = None
itsQuantity = None

newItem = input('enter the new item to add to inventory')
findItem = input('enter the item number to find')

def addItem(newItem):
pass

def lookupItem(findItem):
if findItem:
doSomething()
else:
complain()
addItem(findItem)
 
S

Steven D'Aprano

I dont understand why this is such a big deal. Nor do i understand why
google can't find a reasonable answer. If one can't figure out from the
title what I'm trying to do, then a look at code should firmly plant the
intent. The general idea of the code is, in my opinion, very basic.

I notice, that when you are coding a class instance, it's in the format
of: newInstance = someClass()

OK, so we are calling someClass as if it's a function. Is there a way to
do something to the effect of:
someClass(newInstance)

I don't know. What effect do you expect?

Please don't assume that just because you've spent hours searching for an
answer, we know what the question is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top