Send list of variables one class to other class in python

196 Views Asked by At

Here iam want send one class object properties to other class.my basic program look like here

class A():
    def __init__(self,name,age):
           Self.name = name
           Self .age = age
            Return tuple(self.name,self.age)
Class B():
       def __init__(self,oldInfo,job):
            Self.oldInfo=oldInfo
            Self.job=job
            Name,age= oldInfo
            Print(Name,age,job)
#calling block
Obj1=A("Scott","28")
Obj2=B(Obj1,"devolper") 

So some errors are occurring like

over unpack

should return None not tuple

return should be 1 value not multiple

3

There are 3 best solutions below

0
On BEST ANSWER

so when I answer this question, I think I should give you some advice.

1、python keywords is lower case with first letter, def is not Def.others is also. you can look my code or use this code in the idle.

import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 
'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 
'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 
'while', 'with', 'yield']

2、another is you should use the four space in your code with indent.

3、when you want to get the properties ,you should define the method in your code .the Obj1=A("Scott","28"), Obj1 is only a object with the class A, you can use the object to get your properties with method .

My code:

class A():

    def __init__(self,name,age):
        self.name = name
        self .age = age

    def gettuple(self):
        return tuple([self.name,self.age])
class B():

    def __init__(self,oldInfo,job):

        self.oldInfo = oldInfo
        self.job = job
        (Name,age) = oldInfo
        print(Name,age,job)


#calling block
devolper = 'goggo'
Obj1=A("Scott","28")
Obj2=B(Obj1.gettuple(),devolper) 

last time ,I think your should learn more in python.org with more examples enter link description here

0
On
  1. Tuple should be tuple((self.name,self.age)) instead of tuple(self.name,self.age), because tuple() receives only 1 argument instead of 2.(I am not sure about this on python3)
  2. __init__() should return None instead of a tuple, as it will create an object, try to figure it out.
  3. as a result, oldInfo is "one" object, and you tried to unpack it into 2, name and age, therefore you got an "over unpack" error.
0
On

the __init__() method must always return None. You can't make it return anything else. You can stick your tuple in a instance variable instead and then get that either using a getter method on your instance or (less pythonically) directly. You're also using tuple() incorrectly. tuple() takes a single argument as a tuple or a list, not two separate arguments. Calling tuple() isn't really necessarily either as you can create a new tuple simply by doing foo = ('something', 'something else')

class A():
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.tuple = (self.name, self.age)

class B():
    def __init__(self, oldInfo, job):
        self.oldInfo = oldInfo
        self.job = job
        name, age = oldInfo.tuple
        print(name, age, job)

obj1 = A("Scott", "28")
obj2 = B(obj1, "developer")

Also note that your tuple isn't really necessary as you have already assigned name and age as attributes in your A class. You can just call them from B since B already has a reference to the whole object:

class A():
    def __init__(self, name, age):
        self.name = name
        self.age = age

class B():
    def __init__(self, oldInfo, job):
        self.oldInfo = oldInfo
        self.job = job
        print(oldInfo.name, oldInfo.age, job)

obj1 = A("Scott", "28")
obj2 = B(obj1, "developer")