Jul-20-2021, 08:33 PM
(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)
Output:Help on class AClass in module __main__:
class AClass(builtins.object)
| AClass(situation: bool)
|
| AClass(situation: bool)
|
| Methods defined here:
|
| __eq__(self, other)
|
| __init__(self, situation: bool) -> None
|
| __repr__(self)
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(cls, situation: bool)
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __annotations__ = {'situation': <class 'bool'>}
|
| __dataclass_fields__ = {'situation': Field(name='situation',type=<clas...
|
| __dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,or...
|
| __hash__ = None
Help on class object in module builtins:
class object
| The base class of the class hierarchy.
|
| When called, it accepts no arguments and returns a new featureless
| instance that has no instance attributes and cannot be given any.
|
| Built-in subclasses:
| async_generator
| BaseException
| builtin_function_or_method
| bytearray
| ... and 87 other subclasses
|
| Methods defined here:
|
| __delattr__(self, name, /)
| Implement delattr(self, name).
|
| __dir__(self, /)
| Default dir() implementation.
|
| __eq__(self, value, /)
| Return self==value.
|
| __format__(self, format_spec, /)
| Default object formatter.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __le__(self, value, /)
| Return self<=value.
|
| __lt__(self, value, /)
| Return self<value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __reduce__(self, /)
| Helper for pickle.
|
| __reduce_ex__(self, protocol, /)
| Helper for pickle.
|
| __repr__(self, /)
| Return repr(self).
|
| __setattr__(self, name, value, /)
| Implement setattr(self, name, value).
|
| __sizeof__(self, /)
| Size of object in memory, in bytes.
|
| __str__(self, /)
| Return str(self).
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __init_subclass__(...) from builtins.type
| This method is called when a class is subclassed.
|
| The default implementation does nothing. It may be
| overridden to extend subclasses.
|
| __subclasshook__(...) from builtins.type
| Abstract classes can override this to customize issubclass().
|
| This is invoked early on by abc.ABCMeta.__subclasscheck__().
| It should return True, False or NotImplemented. If it returns
| NotImplemented, the normal algorithm is used. Otherwise, it
| overrides the normal algorithm (and the outcome is cached).
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __class__ = <class 'type'>
| type(object_or_name, bases, dict)
| type(object) -> the object's type
| type(name, bases, dict) -> a new type
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 foo.__new__()
to make a new instance of foo.
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.