1

I am trying to parse the python file using 'ast' and I am able to successfully do it.

Now I need to differentiate the default methods of 'dict' and 'list'

Example :
I can have a method like below

classa = new TestClassA
classa.test()

But If I call a method of 'dict' / 'list'

some_dict = dict() 
some_dict.keys()

I need to identify the default methods of 'dict' and 'list' and skip it

One way is: I can have all the methods in the configuration file and I can skip it, But If there a better to identify it please let me know.

5
  • 1
    What do you mean by "default methods"? Commented Jul 15, 2019 at 14:36
  • @Code-Apprentice I mean to say the functions provided by dict and list like dict.update / dict.keys() etc., Commented Jul 15, 2019 at 14:40
  • classa = new TestClassA is not valid Python; it should rather be classa = TestClassA(). Commented Jul 15, 2019 at 15:21
  • To clarify: As I undertand the question, you want to parse a Python file and identify all the methods of "proper" classes (by whatever definition) as opposed to methods provided by builtin types like dict? This will probably not be possible with static code analysis, as the type of a variable might be determined at run-time. (You could blacklist all builtin methods, but what if another class defines a method of the same name?) Commented Jul 15, 2019 at 15:22
  • @tobias_k yes correct Commented Jul 15, 2019 at 15:24

2 Answers 2

2

You could get a glimpse of methods and attributes of a given object in two simple ways:

  1. Using dir() function on the object or instance of an object
  2. Using help() function

1. dir() function

>>> d = dict()
>>> type(d)
<class 'dict'>
>>> for attribute in dir(d):
...   print(attribute)
... 
__gt__
clear
copy
fromkeys
get
items
keys
pop
popitem
setdefault
update
values

2. help() function

This is probably the most elegant way to print the attributes and functions of an object

class dict(object)
 |  dict() -> new empty dictionary
 |  dict(mapping) -> new dictionary initialized from a mapping object's
 |      (key, value) pairs
 |  dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v
 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs
 |      in the keyword argument list.  For example:  dict(one=1, two=2)
 |  
 |  Methods defined here:
 |  
 |  __contains__(self, key, /)
 |      True if D has a key k, else False.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 ...
 ...
 |  copy(...)
 |      D.copy() -> a shallow copy of D
 |  
 |  fromkeys(iterable, value=None, /) from builtins.type
 |      Returns a new dict with keys from iterable and values equal to value.
 |  
 |  get(...)
 |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
 |  
 |  items(...)
 |      D.items() -> a set-like object providing a view on D's items
 |  
 |  keys(...)
 |      D.keys() -> a set-like object providing a view on D's keys
 |  
 |  pop(...)
 |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
 |      If key is not found, d is returned if given, otherwise KeyError is raised
 |  
 |  popitem(...)
 |      D.popitem() -> (k, v), remove and return some (key, value) pair as a
 |      2-tuple; but raise KeyError if D is empty.
 |  
 |  setdefault(...)
 |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
 |  
 |  update(...)
 |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
 |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
 |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
 |      In either case, this is followed by: for k in F:  D[k] = F[k]
 |  
 |  values(...)
 |      D.values() -> an object providing a view on D's values
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None
Sign up to request clarification or add additional context in comments.

Comments

1

You can get a list of members defined by a class by calling dir(class_name). So for example dir(list) and dir(dict) will give all of the members of each of these two classes. Note that this includes special names like __init__ as well as fields. You will still have to determine which ones you actually want to skip.

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.