Passing variables, creating instances, self, The mechanics and usage of classes: need explenation

Posted by Baf on Stack Overflow See other posts from Stack Overflow or by Baf
Published on 2012-07-10T20:45:45Z Indexed on 2012/07/10 21:15 UTC
Read the original article Hit count: 178

I've been sitting over this the whole day and Im a little tired already so please excuse me being brief.

Im new to python.

I just rewrrote a working program, into a bunch of functions in a class and everzthings messed up.

I dont know if its me but Im very surprised i couldn t find a beginners tutorial on how to handle classes on the web so I have a few questions.

First of all, in the init section of the class i have declared a bunch of variables with self.variable=something.

Is it correct that i should 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. ?

Secondly how do i correctly pass arguments to the class? some example code would be cool.

thirdly how do i call a function of the class outside of the class scope? some example code would be cool.

fouthly how do I create an Instance of the class INITIALCLASS in another class OTHERCLASS, passing variables from OTHERCLASS to INITIALCLASS? some example code would be cool.

I Want to call a function from OTHERCLASS with arguments from INITIALCLASS. What Ive 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 havent understood how to pass variables to classes or how to handle self, when to use it and when not, I probably also havent understood how to properly create an isntance of a class. In general i havent udnerstood the mechanics of classes So please help me and explain it to me like i have no Idea( which i dont it seems). Or point me to a thorough video, or readable tutorial.

All i find on the web is super simple examples, that didnt 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.

Thanks for the Help Much appreciated!

© Stack Overflow or respective owner

Related posts about python

Related posts about class