Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6207925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:43:06+00:00 2026-05-24T05:43:06+00:00

Can anyone explain how to make the following example work? Since several functions inside

  • 0

Can anyone explain how to make the following example work? Since several functions inside of the class will use the same function from platform I thought it would be better to import it right inside of the class, but I don’t see how I can use it inside of the function (since I constantly get errors about it).

#!/usr/bin/python

class test:
   from platform import system
   is_linux(self):
      system = system()
      if system == "Linux": return True

A better example:

#!/usr/bin/python

# Add ANSI colour strings
class stdout:
    from sys import stdout
    def message(message, self):  stdout.write(message)

Note: This is just a snippet, there are some parts missing but an example of what I mean.

I know I could probably just move system = system() and use self.system but perhaps a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:43:07+00:00Added an answer on May 24, 2026 at 5:43 am

    Well, it is not that simple.
    Actually, import statement in many aspects looks like direct definition of something in place of it. If you write

    class test:
        from platform import system
    

    it looks exactly like

    class test:
        def system():
            # ....
    

    and then you have following problems:

    1. you can’t use just system() because system is not in global scope
    2. you can’t use self.system() because in this form, python automatically passes self as first argument, but system() has no parameters and you’ll get TypeError: system() takes no arguments (1 given)
    3. you can’t use test.system() because system() looks like a plain method, and you’ll get TypeError: unbound method system() must be called with test instance as first argument (got nothing instead)

    There are several ways around these problems:

    1. place import platform at top level, and use platform.system() wherever you want, thus fixing issue #1 from prev. list
    2. use staticmethod decorator, fixing issues #2 and #3 from prev. list.

    like

    class test:
        from platform import system
        system = staticmethod(system)
    

    then you can use either self.system() or test.system()

    Actually, you should just import everything in toplevel and forget about it.
    You only have to split import declarations if you need something special for running.
    Like

    import foo
    import bar
    
    def fun(param1, param2):
        # .....
    
    if __name__ == '__main__':
        from sys import argv
        if len(argv) > 2:
            fun(argv[1], argv[2])
    

    In this example, moving from sys import argv is valid, because it is needed only when running a script. But when you use it as an imported module, there is no need in this import.
    But that is not your case, because in your case, system() is always needed for test class, so there is no reason to move this import from toplevel. Just leave it there and never mind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain how to make this code work? echo '<div id=panel1-under>Welcome <?php echo
Can anyone please explain what the following code checks for? I can make no
Can anyone explain the situations in which we need to make use of temporary
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain the following behaviour to me? When a field type in an
Can anyone explain how to make silent (without any user interface) install of .NET
I need to make a Cocoa radio button programmatically, can anyone explain how this
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.