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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:21:20+00:00 2026-06-07T10:21:20+00:00

I just rewrote a working program into functions in a class and everything messed

  • 0

I just rewrote a working program into functions in a class and everything messed up.

First, in the __init__ section of the class I declared a bunch of variables with self.variable=something.

Should I be able to access/modify these variables in every function of the class by using self.variable in that function? In other words, by declaring self.variable I have made these variables, global variables in the scope of the class right?

If not, how do I handle self?

Second, how do I correctly pass arguments to the class?

Third, how do I call a function of the class outside of the class scope?

Fouth, how do I create an Instance of the class INITIALCLASS in another class OTHERCLASS, passing variables from OTHERCLASS to INITIALCLASS?

I want to call a function from OTHERCLASS with arguments from INITIALCLASS. What I’ve done so far is.

class OTHERCLASS():
    def __init__(self,variable1,variable2,variable3):
        self.variable1=variable1
        self.variable2=variable2
        self.variable3=variable3
    def someotherfunction(self):
        something=somecode(using self.variable3)
        self.variable2.append(something)
        print self.variable2
    def somemorefunctions(self):
        self.variable2.append(variable1)
        
class INITIALCLASS():
    def __init__(self):
        self.variable1=value1
        self.variable2=[]
        self.variable3=''
        self.DoIt=OTHERCLASS(variable1,variable2,variable3)

    def somefunction(self):
        variable3=Somecode
        #tried this
        self.DoIt.someotherfunctions()
        #and this
        DoIt.someotherfunctions()

I clearly didn’t understand how to pass variables to classes or how to handle self, when to use it and when not. I probably also didn’t understand how to properly create an instance of a class. In general I didn’t understand the mechanics of classes so please help me and explain it to me like I have no idea (which I don’t, it seems). Or point me to a thorough video, or readable tutorial.

All I find on the web is super simple examples, that didn’t help me much. Or just very short definitions of classes and class methods instances etc.

I can send you my original code if you guys want, but its quite long.

  • 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-07T10:21:22+00:00Added an answer on June 7, 2026 at 10:21 am
    class Foo          (object):
        # ^class name  #^ inherits from object
    
        bar = "Bar" #Class attribute.
    
        def __init__(self):
            #        #^ The first variable is the class instance in methods.  
            #        #  This is called "self" by convention, but could be any name you want.
            #^ double underscore (dunder) methods are usually special.  This one 
            #  gets called immediately after a new instance is created.
    
            self.variable = "Foo" #instance attribute.
            print self.variable, self.bar  #<---self.bar references class attribute
            self.bar = " Bar is now Baz"   #<---self.bar is now an instance attribute
            print self.variable, self.bar  
    
        def method(self, arg1, arg2):
            #This method has arguments.  You would call it like this:  instance.method(1, 2)
            print "in method (args):", arg1, arg2
            print "in method (attributes):", self.variable, self.bar
    
    
    a = Foo() # this calls __init__ (indirectly), output:
                     # Foo bar
                     # Foo  Bar is now Baz
    print a.variable # Foo
    a.variable = "bar"
    a.method(1, 2) # output:
                   # in method (args): 1 2
                   # in method (attributes): bar  Bar is now Baz
    Foo.method(a, 1, 2) #<--- Same as a.method(1, 2).  This makes it a little more explicit what the argument "self" actually is.
    
    class Bar(object):
        def __init__(self, arg):
            self.arg = arg
            self.Foo = Foo()
    
    b = Bar(a)
    b.arg.variable = "something"
    print a.variable # something
    print b.Foo.variable # Foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just about finished my first release of automailer, a program I've been working
just wanted to ask where I define initial class properties? From other languages I
I am trying to write this rewrite code and it just is not working
I just installed Joomla 1.5 and as I am currently working on the website
I've been working on that for 2 days now. Reading everything I could find
I just got done working through the Django tutorials for the second time, and
I've just stated working in haskell semi-explicit parallelism with GHC 6.12. I've write the
I've just setup a wordpress MU install and almost everything seems to be in
Basically, I'm working on a small program in C (again, not a homework task,
i have set in .htaccess Url Rewrite all working just my search function stop

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.