Python Forum

Full Version: How to check if class instance exists in a list of class instance objects?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
(May-26-2018, 08:22 PM)buran Wrote: [ -> ]instances of classes and instances of objects???

Classes in Python are objects too
Of course everything is an object, no doubt about that!!! "
That's exactly why I don't get the difference you make between instances of classes and instances of objects...
(May-27-2018, 03:58 AM)buran Wrote: [ -> ]Of course everything is an object, no doubt about that!!! "
That's exactly why I don't get the difference you make between instances of classes and instances of objects...

Does "object of un-instantiated class" sounds better? If that is still unclear

Output:
In [31]: type(list().__class__) Out[31]: type In [32]: type(dict().__class__) Out[32]: type In [33]: type('abc'.__class__) Out[33]: type
Classes itself are objects of type .... type, which is a trinity Cool of function, metaclass and class factory.

Even if object is un-hashable, the class of that object will be always hashable (unless you implement some crazy hack that will make it unhashable - if that is possible). Again, look at my code: I was not saving objects in a set - I was saving classes


PS Maybe, a little demonstration will help

Output:
In [37]: class A: ...: def __init__(self, name): ...: self.name = name ...: In [38]: set([A('volcano63').__class__, A('buran').__class__, A('sonicblind').__class__]) Out[38]: {__main__.A}
(May-27-2018, 04:32 AM)volcano63 Wrote: [ -> ]Does "object of un-instantiated class" sounds better?
Yes, now I see what you mean
Pages: 1 2 3