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

  • Home
  • SEARCH
  • 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 7592113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:49:31+00:00 2026-05-30T20:49:31+00:00

class MyClass: def myFunc(self): pass Can I create MyFunc() outside of the class definition,

  • 0
class MyClass:
    def myFunc(self):
        pass

Can I create MyFunc() outside of the class definition, maybe even in another module?

  • 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-30T20:49:32+00:00Added an answer on May 30, 2026 at 8:49 pm

    Yes. You can define a function outside of a class and then use it in the class body as a method:

    def func(self):
        print("func")
    
    class MyClass:
        myMethod = func
    

    You can also add a function to a class after it has been defined:

    class MyClass:
        pass
    
    def func(self):
        print("func")
    
    MyClass.myMethod = func
    

    You can define the function and the class in different modules if you want, but I’d advise against defining the class in one module then importing it in another and adding methods to it dynamically (as in my second example), because then you’d have surprisingly different behaviour from the class depending on whether or not another module has been imported.

    I would point out that while this is possible in Python, it’s a bit unusual. You mention in a comment that "users are allowed to add more" methods. That sounds odd. If you’re writing a library you probably don’t want users of the library to add methods dynamically to classes in the library. It’s more normal for users of a library to create their own subclass that inherits from your class than to change yours directly.

    I’d also add a reminder that functions don’t have to be in classes at all. Python isn’t like Java or C# and you can just have functions that aren’t part of any class. If you want to group together functions you can just put them together in the same module, and you can nest modules inside packages. Only use classes when you need to create a new data type, not just to group functions together.

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

Sidebar

Related Questions

I have the following Ruby code: module MyModule class MyClass def self.my_method end end
I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1,
This is my class (as simple as it can be): class MyClass(): def __init__(self,
Look at two ways of structuring my functions: class myClass: def _myFunc(self): pass def
from module import * # adds 'BlahRenderer', 'FooRenderer', 'BarRenderer', etc. class MyClass def __init__(self,
I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
I have a ruby file as follows: module Example class Myclass def t_st Hello
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Consider this example: class MyClass: def func(self, name): self.name = name I know that

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.