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().

4
  • 1
    Possible duplicate of Passing a dictionary to a function in python as keyword parameters Commented Feb 13, 2016 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. :) Commented Feb 13, 2016 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 :-) Commented Feb 13, 2016 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. Commented Feb 13, 2016 at 15:40

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. What would be the 'normal' way, if not this way?
@kirstyhattersley: What you're doing is ok, IMHO. But take a look at the answers on the linked question, and it should give you a few more ideas.
Yes it has. Thanks a lot!
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.

Comments

0

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

**dict or **func()

in a new object call

Comments

0

Please see if this meets your requirements.

It allows real time user inputs to set attributes.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.