Pass a variable from one class to another












0















I am trying to pass one variable from a class to another one. I tried doing it and searched the web but I can't really find out what I was looking for. I have 2 classed names; class1, class2. I am trying to create a variable called self.v in class1 and try to pass it to class2. This is what I have tried so far...



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self):
pass
def print(self,c1):
print(self.v)

c = class1()
c1 = c.create()
c2 = class2()
c3 = c2.print(c1)


I also tried doing this:



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self,class1):
self.print(class1)
def print(self,class1):
print(self.v)

c = class1()
c1 = class2(c)


.. but that didn't really help fix but the problem either. If someone could please help me, I would appreciate a lot. Thank you.










share|improve this question


















  • 1





    Have you already worked through the Python tutorial? If not, you should do before trying more complicated things.

    – Michael Butscher
    Nov 20 '18 at 21:20






  • 2





    Hint: in your first attempt, print takes an argument c1 that you aren't doing anything with. Maybe you should be doing something with it.

    – chepner
    Nov 20 '18 at 21:28
















0















I am trying to pass one variable from a class to another one. I tried doing it and searched the web but I can't really find out what I was looking for. I have 2 classed names; class1, class2. I am trying to create a variable called self.v in class1 and try to pass it to class2. This is what I have tried so far...



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self):
pass
def print(self,c1):
print(self.v)

c = class1()
c1 = c.create()
c2 = class2()
c3 = c2.print(c1)


I also tried doing this:



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self,class1):
self.print(class1)
def print(self,class1):
print(self.v)

c = class1()
c1 = class2(c)


.. but that didn't really help fix but the problem either. If someone could please help me, I would appreciate a lot. Thank you.










share|improve this question


















  • 1





    Have you already worked through the Python tutorial? If not, you should do before trying more complicated things.

    – Michael Butscher
    Nov 20 '18 at 21:20






  • 2





    Hint: in your first attempt, print takes an argument c1 that you aren't doing anything with. Maybe you should be doing something with it.

    – chepner
    Nov 20 '18 at 21:28














0












0








0








I am trying to pass one variable from a class to another one. I tried doing it and searched the web but I can't really find out what I was looking for. I have 2 classed names; class1, class2. I am trying to create a variable called self.v in class1 and try to pass it to class2. This is what I have tried so far...



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self):
pass
def print(self,c1):
print(self.v)

c = class1()
c1 = c.create()
c2 = class2()
c3 = c2.print(c1)


I also tried doing this:



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self,class1):
self.print(class1)
def print(self,class1):
print(self.v)

c = class1()
c1 = class2(c)


.. but that didn't really help fix but the problem either. If someone could please help me, I would appreciate a lot. Thank you.










share|improve this question














I am trying to pass one variable from a class to another one. I tried doing it and searched the web but I can't really find out what I was looking for. I have 2 classed names; class1, class2. I am trying to create a variable called self.v in class1 and try to pass it to class2. This is what I have tried so far...



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self):
pass
def print(self,c1):
print(self.v)

c = class1()
c1 = c.create()
c2 = class2()
c3 = c2.print(c1)


I also tried doing this:



class class1(object):
def __init__(self):
self.create()
def create(self):
self.v = 6

class class2(object):
def __init__(self,class1):
self.print(class1)
def print(self,class1):
print(self.v)

c = class1()
c1 = class2(c)


.. but that didn't really help fix but the problem either. If someone could please help me, I would appreciate a lot. Thank you.







python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 21:16









AR_AR_

616




616








  • 1





    Have you already worked through the Python tutorial? If not, you should do before trying more complicated things.

    – Michael Butscher
    Nov 20 '18 at 21:20






  • 2





    Hint: in your first attempt, print takes an argument c1 that you aren't doing anything with. Maybe you should be doing something with it.

    – chepner
    Nov 20 '18 at 21:28














  • 1





    Have you already worked through the Python tutorial? If not, you should do before trying more complicated things.

    – Michael Butscher
    Nov 20 '18 at 21:20






  • 2





    Hint: in your first attempt, print takes an argument c1 that you aren't doing anything with. Maybe you should be doing something with it.

    – chepner
    Nov 20 '18 at 21:28








1




1





Have you already worked through the Python tutorial? If not, you should do before trying more complicated things.

– Michael Butscher
Nov 20 '18 at 21:20





Have you already worked through the Python tutorial? If not, you should do before trying more complicated things.

– Michael Butscher
Nov 20 '18 at 21:20




2




2





Hint: in your first attempt, print takes an argument c1 that you aren't doing anything with. Maybe you should be doing something with it.

– chepner
Nov 20 '18 at 21:28





Hint: in your first attempt, print takes an argument c1 that you aren't doing anything with. Maybe you should be doing something with it.

– chepner
Nov 20 '18 at 21:28












1 Answer
1






active

oldest

votes


















1














If class2 needs an instance of class1, one obvious solution is to pass the class1 instance to the class2 initializer:



class Class1(object):
def __init__(self, v):
self.v = v

class Class2(object):
def __init__(self, c1):
self.c1 = c1
def print(self):
print(self.c1.v)

c1 = Class1(6)
c2 = Class2(c1)
c2.print()





share|improve this answer


























  • thanks man, this really helped me a lot.

    – AR_
    Nov 20 '18 at 21:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401656%2fpass-a-variable-from-one-class-to-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If class2 needs an instance of class1, one obvious solution is to pass the class1 instance to the class2 initializer:



class Class1(object):
def __init__(self, v):
self.v = v

class Class2(object):
def __init__(self, c1):
self.c1 = c1
def print(self):
print(self.c1.v)

c1 = Class1(6)
c2 = Class2(c1)
c2.print()





share|improve this answer


























  • thanks man, this really helped me a lot.

    – AR_
    Nov 20 '18 at 21:41
















1














If class2 needs an instance of class1, one obvious solution is to pass the class1 instance to the class2 initializer:



class Class1(object):
def __init__(self, v):
self.v = v

class Class2(object):
def __init__(self, c1):
self.c1 = c1
def print(self):
print(self.c1.v)

c1 = Class1(6)
c2 = Class2(c1)
c2.print()





share|improve this answer


























  • thanks man, this really helped me a lot.

    – AR_
    Nov 20 '18 at 21:41














1












1








1







If class2 needs an instance of class1, one obvious solution is to pass the class1 instance to the class2 initializer:



class Class1(object):
def __init__(self, v):
self.v = v

class Class2(object):
def __init__(self, c1):
self.c1 = c1
def print(self):
print(self.c1.v)

c1 = Class1(6)
c2 = Class2(c1)
c2.print()





share|improve this answer















If class2 needs an instance of class1, one obvious solution is to pass the class1 instance to the class2 initializer:



class Class1(object):
def __init__(self, v):
self.v = v

class Class2(object):
def __init__(self, c1):
self.c1 = c1
def print(self):
print(self.c1.v)

c1 = Class1(6)
c2 = Class2(c1)
c2.print()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 21:37

























answered Nov 20 '18 at 21:29









John GordonJohn Gordon

9,57551729




9,57551729













  • thanks man, this really helped me a lot.

    – AR_
    Nov 20 '18 at 21:41



















  • thanks man, this really helped me a lot.

    – AR_
    Nov 20 '18 at 21:41

















thanks man, this really helped me a lot.

– AR_
Nov 20 '18 at 21:41





thanks man, this really helped me a lot.

– AR_
Nov 20 '18 at 21:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401656%2fpass-a-variable-from-one-class-to-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy