calling private methods for class method: python -


I'm trying to implement several constructors in Python and one of the suggestions (via online search) classmethod Was to use. However, using it, I have problems with code reuse and modularity. Here is an example where I can create an object based on a supplied file or through another medium: def (__init __ (self, filename): self.image = lib.load filename) self.init_others () @ data_class_de_tata (cls, data, header) from def: cls.image = Lib.from_data (data, header) cls.init_others () return cos def init_others (self): # some other variables themselves start. Some = numpy.matrix (4,4)

now Like this So that I can not do that. Cls.init_others () call fails by saying that I have not provided the object to call it. I think I can start these things in an in-data function but then I repeat the code in the init method and the other "constructor". Does anyone know how I can call these other initial methods with these @ classmethod works? Or maybe someone knows a better way of starting these variables.

I came from C ++ background. So even now Python is trying to find my way around construction.

Your class method should be returned by making a new instance of the class, class attribute should not be specified As an alternative to class keyword logic itself, you can do something like this:

  class image: def __init __ (self, image): self.image = image self.init_others () @ Classmethod def from_data: return cls (lib.from_data (data, header)) @classmethod def from_filename (cls, filename): return cls (lib.load (filename)) def init_others (self): # Some other variables are initial self.something = numpy.matrix (4, 4)   

Adds the ability to create an anvil if you already have image

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -