Search code examples
python-3.xpython-2.7listvaluetuple

Send list of variables one class to other class in python


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


Solution

  • 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