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 75007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:26:54+00:00 2026-05-10T20:26:54+00:00

Say I am declaring a class C and a few of the declarations are

  • 0

Say I am declaring a class C and a few of the declarations are very similar. I’d like to use a function f to reduce code repetition for these declarations. It’s possible to just declare and use f as usual:

>>> class C(object): ...     def f(num): ...             return '<' + str(num) + '>' ...     v = f(9) ...     w = f(42) ...  >>> C.v '<9>' >>> C.w '<42>' >>> C.f(4) Traceback (most recent call last):   File '<stdin>', line 1, in <module> TypeError: unbound method f() must be called with C instance as first argument (got int instance instead) 

Oops! I’ve inadvertently exposed f to the outside world, but it doesn’t take a self argument (and can’t for obvious reasons). One possibility would be to del the function after I use it:

>>> class C(object): ...     def f(num): ...             return '<' + str(num) + '>' ...     v = f(9) ...     del f ...  >>> C.v '<9>' >>> C.f Traceback (most recent call last):   File '<stdin>', line 1, in <module> AttributeError: type object 'C' has no attribute 'f' 

But what if I want to use f again later, after the declaration? It won’t do to delete the function. I could make it ‘private’ (i.e., prefix its name with __) and give it the @staticmethod treatment, but invoking staticmethod objects through abnormal channels gets very funky:

>>> class C(object): ...     @staticmethod ...     def __f(num): ...             return '<' + str(num) + '>' ...     v = __f.__get__(1)(9)   # argument to __get__ is ignored... ...  >>> C.v '<9>' 

I have to use the above craziness because staticmethod objects, which are descriptors, are not themselves callable. I need to recover the function wrapped by the staticmethod object before I can call it.

There has got to be a better way to do this. How can I cleanly declare a function in a class, use it during its declaration, and also use it later from within the class? Should I even be doing this?

  • 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. 2026-05-10T20:26:55+00:00Added an answer on May 10, 2026 at 8:26 pm

    Quite simply, the solution is that f does not need to be a member of the class. I am assuming that your thought-process has gone through a Javaish language filter causing the mental block. It goes a little something like this:

    def f(n):     return '<' + str(num) + '>'  class C(object):      v = f(9)     w = f(42) 

    Then when you want to use f again, just use it

    >>> f(4) '<4>' 

    I think the moral of the tale is ‘In Python, you don’t have to force everything into a class’.

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

Sidebar

Related Questions

quick question on declaring functions. Lets say I have the following code: class a{
Is there a more efficient way of declaring and using these (very similar/repetitive) CSS
Say i have a few fields like the following: abd738927 jaksm234234 hfk342 ndma0834 jon99322
Say I have classes class A{ //code for class A } class B{ //code
I have a particular scenario below. The code below should print 'say()' function of
Say you're declaring a class with all the bells and whistles - constructor and
Let’s say you wish to have two classes like so, each declaring an instance
Lets say I have a set of model classes like this: public class Person
Let's say I have 2 classes like this: public class A { public string
Say I'm declaring a class Game . class @Game constructor: -> @id = Game.generateNewGameId()

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.