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: 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: Adds the ability to create an anvil if you already have
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)
image
Comments
Post a Comment