class needs to refer a different class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: class needs to refer a different class (/thread-34295.html) |
RE: class needs to refer a different class - Skaperen - Jul-19-2021 i'm still a bit confused with that code. it's more about the __new__() method. i don't understand passing cls to it. all i need is a method with no args. i am reading docs for 3.7 and coding for 3.6. RE: class needs to refer a different class - Yoriz - Jul-19-2021 https://docs.python.org/3/reference/datamodel.html?highlight=__new__#object.__new__ RE: class needs to refer a different class - Skaperen - Jul-20-2021 what is the difference between object and cls? what i was expecting was foo.__new__() to make a new instance of foo.
RE: class needs to refer a different class - Yoriz - Jul-20-2021 (Jul-20-2021, 01:20 AM)Skaperen Wrote: what is the difference between object and cls? from dataclasses import dataclass @dataclass class AClass: situation: bool def __new__(cls, situation: bool): help(cls) help(object) if situation: return DifferentClass(situation) return super().__new__(cls) @dataclass class DifferentClass: situation: bool a_class = AClass(situation=False) # different_class = AClass(situation=True) # print(a_class) # print(different_class)
object is the built in object that all classes subclasscls is the parameter used in a class __new__ method it is a pointer to the class itself just like self is a pointer to the instance of a class. (Jul-20-2021, 01:20 AM)Skaperen Wrote: what i was expecting was Where does foo come into it? you have not mentioned foo until now or given a basic example of your classes yet or modified my version to your needs or closest you feel you can to your needs.
RE: class needs to refer a different class - Gribouillis - Jul-20-2021 I think there is way too much code in _zio.__init__() and most of this code is not directly related to the instance being created. Only three attributes are set in this method, namely self.realname, self.tmpname, self.allfiles . I think most of this method's code should be written in an external "factory" function named for example _new_io() and the structure could bedef _new_io(name=None,mode='r',compresslevel=None,*, buffering=-1, compress=None, closefd=True, check=-1, encoding=None, errors=None, format=None, newline=None, tempname=False, ): ... zio = _zio(realname, tmpname, allfiles) ... return zio class _zio(io.IOBase): def __init__(self, realname, tmpname, allfiles): self.realname = realname self.tmpname = tmpname self.allfiles = allfiles ... zopen() and ztopen() would invoke _new_io() instead of _zio() and the advantage is that the body of _new_io() can now incorporate special cases where it returns something different from a _zio() instance, perhaps another instance of io.IOBase() .
RE: class needs to refer a different class - Skaperen - Jul-21-2021 normally foo just means something arbitrary. in this case it was the class the caller was expecting to get an instance of, the method which would choose to make an instance of some other class that caller does not know about. a poor example of this is a class that opens a file that may, or may not, be filtered in some way. if the method determines that no filtering is needed, that the objective is to return an instance of file class instead of an instance of itself (it would do the filtering, if needed). i give this example as a way to understand what i'm trying to do, not the details to find some other way to do this, i was originally trying to do this with a front end function to decide which class is needed. RE: class needs to refer a different class - Gribouillis - Jul-21-2021 Skaperen Wrote:the objective is to return an instance of file class instead of an instance of itselfI'm not sure I understand what you are trying to do but calling a class' constructor to get an object of a different type seems completely inappropriate. More specifically, __init__() methods don't even use the return statement in Python.You could achieve the same (misleading) effect by overwriting the name of the class class Foo: pass class Bar: pass _c_foo = Foo def Foo(condition): if condition: return _c_foo() else: return Bar() if __name__ == '__main__': f = Foo(0) # returns a Bar instance while pretending to create a Foo instance. RE: class needs to refer a different class - Skaperen - Jul-21-2021 the intention is to provide an instance that is appropriate for the particular file. if an instance of io.IOBase is appropriate, that's what should be returned. but, an instance of this class may be appropriate. RE: class needs to refer a different class - Gribouillis - Jul-21-2021 This is not the role of an __init__() method. The purpose of this method is to initialize the state of a new instance of the class where it belongs.
RE: class needs to refer a different class - Skaperen - Jul-21-2021 right. i am thinking i should just have functions zopen , ztopen , and topen to pass the varying parameters to the class. the difficulty is the complex relation of the file name to the sequence of (de)compression opens to be done for files that are multiply compressed. i know i can set that up in a function, but making it all into a single class that can close() them all in the right order is really messy.and, i am looking at making zpopen() that sets up the compress as process for better performance in certain cases.and, yes, i really do have a few multiply compressed files. i originally was wondering in any of the (de)compression classes have issues with multiple instances of itself for the same file. they shouldn't. they didn't. gzip.open() really can open against another instance of itself (for "foo.gz.gz") just fine.
|