1

I am trying to pass a dict variable into a function in Python 3 and then trying to iterate through the keys by calling the .keys() function. However I don't know how to specify the type of the parameter.

def DisplayStock(StockDict):
    for key in StockDict.keys():

The error I am getting is

for key in StockDict.keys():
AttributeError: 'function' object has no attribute 'keys'
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Stuart Maher
  • 23
  • 1
  • 4
  • please elaborate, it's very unclear. do you mean the type of the argument `StockDict` so that you know it's a dictionary? If that is the case, python adopts the philosophy of `duck typing`, see [here](https://en.wikipedia.org/wiki/Duck_typing). So as long as the object passed support the appropriate protocols, you can consider it a dictionary. See [here](https://docs.python.org/3/c-api/abstract.html) and look for other similar Q&A on SO and the internet – Pynchia Nov 18 '15 at 11:31
  • 1
    Possible duplicate of [Python and dictionary like object](http://stackoverflow.com/questions/1277881/python-and-dictionary-like-object) – Pynchia Nov 18 '15 at 11:41
  • I am passing it a dict. However, I am unable to use the .keys() method as the interpreter doesn't realise it's a dict, it seems to be throwing an error... At least that's what I think is happening! – Stuart Maher Nov 18 '15 at 11:46
  • 1
    "At least that's what I think is happening!" What do you mean by thinking? If your code doesn't run you should see an exception and stack trace that tells you exactly what is happening. – DeepSpace Nov 18 '15 at 11:47
  • Please show the `StockDict` type: `print(type(StockDict))` – Tomasz Jakub Rup Nov 18 '15 at 11:53
  • @StuartMaher no, what's happening is that you **aren't passing it a `dict`**. – jonrsharpe Nov 18 '15 at 11:53
  • What I mean by thinking.. is.. thinking... I've never come across a language where the error messages are clear and unambiguous. As such, when you read them, you have to think. I've added the error message above. It was my bad not have added it form the start. – Stuart Maher Nov 18 '15 at 11:54
  • @StuartMaher you are simply not passing a dictionary it is a function – The6thSense Nov 18 '15 at 11:56
  • *sigh* Looking back through the code where I generate the dictionary data I reference the function, but no brackets, so yes, I am passing it a function. – Stuart Maher Nov 18 '15 at 12:01

3 Answers3

0

It seems like you are just passing something wrong to the function. From the error it seems like you are giving it a function. Maybe you should put () behind the argument that is causing the error.

Normally you don't need to specify a type. The function will take any type.

If you want to check if the passed argument is a dict you could use:

if isinstance(Stockdict, dict):
     for key in Stockdict.keys()

This is not very pythonic though. Just don't pass a non dict like object to the function.

JelteF
  • 3,021
  • 2
  • 27
  • 35
  • please elaborate, it's an opportunity for a good answer, although it does sound like a duplicate... – Pynchia Nov 18 '15 at 11:36
  • `if isinstance(dict):` should be `if isinstance(StockDict, dict):` – DeepSpace Nov 18 '15 at 11:43
  • It's not runtime stype safety I am concerned about. The program won't go through the interpreter. I've edited the original question to include the error as that may shed light (I should have done it from the start, bad me!) – Stuart Maher Nov 18 '15 at 11:51
0

I guess You ask about pep-484

def DisplayStock(StockDict: dict):
    for key in StockDict.keys()

StockDict is a parameter name, dict is a parameter type.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
0

The problem is not in the parameter type. Python is happy for just a variable name and as the original code and then use the .keys method. The error was in a different part of the code I missed brackets on a function call and had set my dictionary object to be a function reference.

Stuart Maher
  • 23
  • 1
  • 4