[Python] vars.items???

Marco De Paoli depaolim a gmail.com
Ven 24 Apr 2015 12:06:15 CEST


Il giorno 24 aprile 2015 11:51, Gabriele Battaglia <iz4apu a libero.it> ha
scritto:

> Scrivo questo codice e non capisco il perchè dell'errore finale.
>
> >>> d={1:34,2:22}
> >>> d
> {1: 34, 2: 22}
> >>> for k,v in d.items():
> ...     print k,v
> ...
> 1 34
> 2 22
> >>> for k,v in vars.items():
> ...     print k,v
> ...
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> AttributeError: 'builtin_function_or_method' object has no attribute
> 'items'
> ***
>

in effetti vars è una funzione built-in
https://docs.python.org/2/library/functions.html#vars

per cui quello che ti sta dicendo python è giusto


> Ma vars risulta di type dict(), quindi... non dovrebbe avere anche lui il
> suo bravo .items?
>

in realtà il valore di ritorno di vars è di tipo dict
vars, in se, è una funzione, per cui devi chiamarla passandogli un
argomento che abbia un attributo __dict__

E se non ce l'ha, come faccio ad iterare attraverso gli elementi di vars?
>

ma tu cosa volevi fare esattamente?
d.items non ti andava bene?


> Grazie come sempre.
>
> GB.
>

ciao,
Marco
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.python.it/pipermail/python/attachments/20150424/edaaf8e5/attachment.html>


Maggiori informazioni sulla lista Python