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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:55:32+00:00 2026-06-16T05:55:32+00:00

I am currently studying programming and I have the following problem. A user should

  • 0

I am currently studying programming and I have the following problem.

A user should enter the operation that he wants to do from the command line and according to the operation, he should enter the information about the item.

I have tried things like the following:

def stock(f):
    d={}
    f()
    return d


def add(item,amount):
    d[item]=amount
    return d

Or

def stock(item,amount,f):
    d={}
    if f():
        d[item]=amount
    return d


def add():
    return True

and, second one doesn’t work for me because, when i want to use this code remove(a,b) i don’t need three argument. What i’m trying to say is number of argument changes according to my operations. How do i handle 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. Editorial Team
    Editorial Team
    2026-06-16T05:55:33+00:00Added an answer on June 16, 2026 at 5:55 am

    If I understand you correctly, you need a dictionary factory pattern

    >>> class Calc(object):
        def __init__(self):
                #Initialize a dictionary of keywords and callbacks
            self.operation = {
                'add':self.add,
                'sub':self.sub,
                'mul':self.mul,
                'div':self.div,
                'neg':self.neg
    
                }
        #Overload the function call
        def __call__(self, *args):
            return self.operation[args[0]](*args[1:])
        # *args - variable list argument list
        def add(self,*args):
            return sum(args)
        def sub(self,*args):
            return args[1] - args[0]
        def mul(self, *args):
            if len(args) == 0:
                return 1
            elif len(args) == 1:
                return args[0]
            else:
                return args[0] * args[1]
        def div(self, *args):
            return args[0] / args[1]
        def neg(self, *args):
            return -args[0]
    
    
    >>> calc = Calc()
    >>> calc('add',1,2,3)
    6
    >>> calc('mul',10)
    10
    >>> calc('mul',10,4)
    40
    >>> calc('neg',3)
    -3
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently studying SQLite and I have found that it uses various classes
I'm currently studying debuggers. I've read that the debuggers have software breakpoints ( apparently
I'm currently studying socket programming, i'm working on creating an application to use my
am currently studying jquery, can someone tell me the things that javascript native can
I am currently studying for an exam I'll have on x86 assembly. I didn't
I am currently studying recursion in school, and I have trouble thinking about methods
Hi i am currently studying sql and I have 2 one to many tables
I am currently studying COM. I found that COM DLL is kind of built
currently i'm studying MySQL, sorry for my newbie question. Here my question I have
I'm currently studying Aspect-Oriented Programming and while I was trying to practice a little

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.