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

Thursday, August 25, 2022

[FIXED] How to invoke a Python method using its fully qualified name?

 August 25, 2022     invoke, methods, module, python     No comments   

Issue

In Java I can invoke a class or method without importing it by referencing its fully qualified name:

public class Example {
    void example() {

        //Use BigDecimal without importing it
        new java.math.BigDecimal(1);
    }
}

Similar syntax will obviously not work using Python:

class Example:
    def example(self):

        # Fails
        print(os.getcwd())

Good practice and PEP recommendations aside, can I do the same thing in Python?


Solution

A function does not exist until its definition runs, meaning the module it's in runs, meaning the module is imported (unless it's the script you ran directly).

The closest thing I can think of is print(__import__('os').getcwd()).



Answered By - Alex Hall
Answer Checked By - David Marino (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