0

I put together a small test code just to demonstrate the error I am getting when trying to enter a class with input. Nothing pretty or good its just to get the specific error across.

Code:

class people(object):
    def deposit():
        print("depositing")
    def withdraw():
        print("withdrawing")
John = people()
selection = input("Type John: ")
selection.deposit

Error:

[evaluate classes.py]
Type John: John
Traceback (most recent call last):
  File "c:\Users\Peter\Desktop\Desktop 2\Python\classes.py", line 9, in module
    selection.deposit
builtins.AttributeError: 'str' object has no attribute 'deposit'
Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
  • 1
    what do you mean by `enter a class with input`? This is a classic [XY problem](http://xyproblem.info/). Please describe what you are trying to achieve – Pynchia Oct 27 '15 at 22:41

4 Answers4

1

If it's only for demonstration purposes you could lookup the selection value in the locals() dict:

In [4]: John = 1

In [5]: selection = "John"

In [6]: locals()[selection]
Out[6]: 1

So, your code would look like:

class people(object):
    def deposit(self):
        print("depositing")
    def withdraw(self):
        print("withdrawing")
John = people()
selection = input("Type John: ")
locals()[selection].deposit()

However, please do not use this approach in production code. There are better patterns for dispatching stuff to objects..

tuxtimo
  • 2,730
  • 20
  • 29
  • Did you test the code before posting? The functions should have `self` as the minimum argument and `locals()[selection].deposit` should be `locals()[selection].deposit()` – Swastik Padhi Oct 27 '15 at 22:51
0

Generally, you don't want this kind of direct correspondence between user input and your internal variable names. You'd have some data structure to keep track of your people instances, perhaps a dict:

known_people = {'John': people(), 'Jake': people(), 'Jeffrey', people()}

and you'd process user input to determine what to do with your data:

selected_person = known_people[selection]
selected_person.deposit()

While you technically can access variables dynamically:

John = people()
selected_person = locals()[selection]

you really, really shouldn't. It doesn't scale well to more complex data, and it introduces potential for lots of nasty bugs.

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

You could store the people (like John) in a separate object (like a dictionary).

Consider that approach:

class Man(object):
    # I renamed the `people` class to `Man` since it's representing a single man (like John).
    # You also have to add a required `self` parameter for every function inside of the class!
    def deposit(self):
        print("depositing")

    def withdraw(self):
        print("withdrawing")

# We'll make `people` a dictionary instead.
people = {
    'John': Man()
}

selection = raw_input("Type John: ")

# You have to put parentheses there so the `deposit()` function would call
people[selection].deposit()

eval() is pretty evil, so as locals().

Community
  • 1
  • 1
Igor Hatarist
  • 5,234
  • 2
  • 32
  • 45
-1

input("Type John: ") returns a string, not an object of type people so you can't call the method deposit on it.

Workaround-

class people(object):
    def deposit(self):
        print("depositing")
    def withdraw(self):
        print("withdrawing")
selection = input("Type people(): ")
selection.deposit()

Input-

people()  

Output-

depositing

Screenshot

Swastik Padhi
  • 1,849
  • 15
  • 27