-1

I have an object called Language:

class Language():
    niceGreetings = ["Hello", "Hi"]
    referToSelf = ["Me", "I"]
    referToCollective = ["We", "Us"]
    referToObject = ["The","It"]
    travelPastTense = ["Went"]
    directionNonSpecific = ["To", "From"]

I want to get a function that does this, basically:

<< listLists(Language)
>> ["niceGreetings", "referToSelf", "referToCollective", etc.]

It pretty much has to be compatible between Python 2 and 3, but I am a little flexible on that

AlgoRythm
  • 115
  • 10
  • 2
    You could try something like `print([x for x in dir(Language) if not x.startswith("__")])`... but what do you _need_ this for, in the first place? Maybe a dictionary would be better. – tobias_k Nov 18 '15 at 19:22

2 Answers2

1

Start with dir(Language).

Output:

['__doc__',
 '__module__',
 'directionNonSpecific',
 'niceGreetings',
 'referToCollective',
 'referToObject',
 'referToSelf',
 'travelPastTense']

You can filter the result as follows:

[field for field in dir(Language) if not field.startswith("__")]

Output:

['directionNonSpecific',
 'niceGreetings',
 'referToCollective',
 'referToObject',
 'referToSelf',
 'travelPastTense']
Falko
  • 17,076
  • 13
  • 60
  • 105
  • my output is actually `['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'directionNonSpecific', 'niceGreetings', 'referToCollective', 'referToObject', 'referToSelf', 'travelPastTense']` for some reason – AlgoRythm Nov 18 '15 at 19:23
  • did you use the second line of code he put up? – letsc Nov 18 '15 at 19:25
0

You could get the list of all attributes on an object and filter for listiness:

[v for v in Language.__dict__.items() if isinstance(v, list)]
a p
  • 3,098
  • 2
  • 24
  • 46