Python Forum
class needs to refer a different class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class needs to refer a different class
#14
(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 subclass
cls 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.
Reply


Messages In This Thread
RE: class needs to refer a different class - by Yoriz - Jul-20-2021, 08:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  is ValueError a class? Skaperen 11 2,679 Mar-29-2023, 02:25 AM
Last Post: Skaperen
  extracting a function/class/method from code Skaperen 5 2,346 Mar-30-2022, 12:13 AM
Last Post: Skaperen
  first class objects Skaperen 0 972 Jan-22-2022, 02:53 AM
Last Post: Skaperen
  returning a different class Skaperen 4 2,173 Oct-20-2021, 12:51 AM
Last Post: Skaperen
  getting my head arounnd __enter__() for my new class Skaperen 5 2,593 Nov-30-2020, 09:46 AM
Last Post: Gribouillis
  find the class for indexed counting Skaperen 4 2,061 Sep-29-2020, 03:26 AM
Last Post: Skaperen
  namespaces when defining a class Skaperen 3 2,169 Jul-03-2020, 06:34 PM
Last Post: Gribouillis
  a file-like class implementation Skaperen 2 2,101 Apr-22-2020, 02:59 AM
Last Post: Skaperen
  making a generator class? Skaperen 2 2,112 Apr-01-2020, 12:34 AM
Last Post: Skaperen
  single-instance class Skaperen 3 2,583 Mar-05-2020, 12:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020