PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 24, 2022

[FIXED] How to check if a python class or object is user defined (not a builtin)?

 August 24, 2022     module, python, types     No comments   

Issue

I need to examine a python module to find simple data as well as "user defined objects". Here, I mean objects (or functions or classes) that are not builtin, but defined with a class/def statement or dynamically loaded/generated.

Note there are similar questions, but they ask about "new style user defined classes". I've seen the inspect module - I couldn't see how it could help.

I can import a module and 'walk' it, but I don't know an easy way to identify an attribute as a simple type. eg - how do I tell 0. is a builtin type? here:

>>> a=0.
>>> dir(a)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__'...
>>> type(a)
<class 'float'>

Do I have to know the classes of all the standard objects to determine if an attribute of a module/object is "user defined"?


Solution

Try checking if the types module is builtin, usually works for me.

For example:

a = 1.2
type(a).__module__ == "__builtin__"


Answered By - maor10
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing