class - returning instances in python -


class classname():      def func(self,a,b):          self.c = a+b          self.d = a-b          self.e = a*b      return self cn = classname() 

this way can access cn.c, cn.d , cn.e can use else other self return , structure. know possible in matlab can define structure in function. expect should this:

class classname():      def func(self,newself,a,b):          self.c = a+b          self.d = a-b          newself.e = a*b      return self, newself cn = classname() 

i know not valid code idea want code.

i think want this:

class classname:     def __init__(self, a, b):         self.c = a+b         self.d = a-b         self.e = a*b cn = classname(12, 34)  # random values 'a' , 'b'. use whatever like! print(cn.c) >>> 46 print(cn.d) >>> -22 print(cn.e) >>> 408 

the __init__ function automatically called when object created. self refer object, adding attributes add object, don't need return anything.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -