Python Forum
How to check if class instance exists in a list of class instance objects?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if class instance exists in a list of class instance objects?
#21
(May-26-2018, 08:22 PM)buran Wrote: instances of classes and instances of objects???

Classes in Python are objects too
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#22
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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#23
(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}
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#24
(May-27-2018, 04:32 AM)volcano63 Wrote: Does "object of un-instantiated class" sounds better?
Yes, now I see what you mean
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python inner classes inheritance from parent class Abedin 8 472 Today, 05:56 AM
Last Post: Gribouillis
  how to get qualified objects in a list Azdaghost 5 380 Yesterday, 06:03 PM
Last Post: Azdaghost
  Accessing method attributes of python class Abedin 6 856 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,235 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,380 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 980 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 3,743 Jul-17-2024, 11:12 AM
Last Post: felixandrea
Bug Copying methods to effect the new owner instead of the old instance Daniel285 2 987 Jun-03-2024, 07:58 AM
Last Post: Gribouillis
  [split] Class and methods ebn852_pan 15 3,334 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,309 May-13-2024, 05:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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