1

I have a couple of questions. The code below doesn't run because I've specified three arguments to the __init__ method and the make_dog function returns a dictionary.

class Dog():
    def __init__(self, name, colour, sex):
        self._name = name
        self._colour = colour
        self._sex = sex

    def __str__(self):
        return '{}, {}, {}'.format(self._name, self._colour, self._sex)

def make_dog():
    user_dog = {}
    yes_or_no = input('Would you like to make a dog?! ')
    if 'y' in yes_or_no:
        print('Great! Let\'s get started')
        user_dog['name'] = input('What shall we call the dog: ')
        user_dog['colour'] = input('What colour is the dog: ')
        user_dog['sex'] = input('What sex is the dog: ')
        return user_dog
    else:
        print('Come back when you want to make a dog. Bye...')
        exit()

d = Dog(make_dog())
print(d)

Here is the output:

Would you like to make a dog?! y
Great! Let's get started
What shall we call the dog: doggie
What colour is the dog: white
What sex is the dog: male
Traceback (most recent call last):
  File "test.py", line 25, in <module>
    d = Dog(make_dog())
TypeError: __init__() missing 2 required positional arguments: 'colour' and 'sex'
  1. What would be the best/standard way to create a dog based on user input as I'm trying to achieve?

  2. Is using dictionaries like in the make_dog function a recommended way of storing and returning values from a function? I think it is but want to check.

Many thanks.

EDIT: I don't think this is a duplicate of Passing a dictionary to a function in python as keyword parameters because I think my question is more beginner centric and specific. Hopefully some other beginner will find this question when wondering how to make an instance from using input().

Community
  • 1
  • 1
  • 1
    Possible duplicate of [Passing a dictionary to a function in python as keyword parameters](http://stackoverflow.com/questions/334655/passing-a-dictionary-to-a-function-in-python-as-keyword-parameters) – PM 2Ring Feb 13 '16 at 13:58
  • 1
    I agree that "Passing a dictionary to a function in python as keyword parameters" is probably _not_ suitable for beginners, and I wouldn't have expected you to find it without knowing what you're looking for. And even then it probably wouldn't help you much without further explanation. Which is why I don't feel too guilty about answering your question, rather than simply closing it as a duplicate. :) – PM 2Ring Feb 13 '16 at 14:21
  • kirsty, it is a dupe of the post that @PM2Ring has linked. Furthermore, duplicate posts are not bad at all. Remember that as you said a beginner might find your post, the beginner might also be interested in the linked post. That said if closed as dupe and someone sees the other post they might come back and see your post as it will be in the Linked post section of the other question. I hope you understood :-) – Bhargav Rao Feb 13 '16 at 15:18
  • Ah sorry. The message suggested editing my question to explain why it wasn't a dupe of the one linked by PM 2Ring. – kirsty hattersley Feb 13 '16 at 15:40

4 Answers4

5

Yes, you can do this, but you have to use the ** operator to convert the dict to an argument list.

d = Dog(**make_dog())

Please see What does ** (double star) and * (star) do for Python parameters? for further info.

Also see Unpacking Argument Lists in the official Python tutorial.

Community
  • 1
  • 1
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
1

As written, the class can't unpack your dictionary. You could replace this line

d = Dog(make_dog())

With

user_dog = make_dog()
d = Dog(user_dog['name'], user_dog['colour'], user_dog['sex']

That said, it's kind of messy. You probably shouldn't even bother with the dictionary in the first place. And you should probably create the dog object within the function.

Tom
  • 1,196
  • 2
  • 12
  • 29
0

Your function returns a dicitionary and class should takie argunents. Use

**dict or **func()

in a new object call

minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57
0

Please see if this meets your requirements.

It allows real time user inputs to set attributes.

https://stackoverflow.com/a/69235456/16829896

Ad Tel
  • 1
  • 1