Python Forum
1 == 1 seems to yield false - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: 1 == 1 seems to yield false (/thread-15185.html)



1 == 1 seems to yield false - zatlas1 - Jan-07-2019

I am a Python newbie. I plucked some code from the internet and trying to build something that resemble the class I am supposed o build

Here is the code:
    def _factory_method(self, name = 'ConcreteProduct1'):
#
        print (name, prodlist[name])
        self.id = prodlist[name]
        localid = self.id
        print (localid)
        if localid == 1:
            print ("Created p1")
            return ConcreteProduct1()
        elif localid == 2:
            print ("Created p2")
            return ConcreteProduct2()
        else:
            print ("no  product")
        print (vars(self))
            

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, value):
        self._id = value
When I run it I get:
ConcreteProduct1 1
1
no product
{'_id': '1'}

which implies that either localid has changed value or that 1 == 1 means false!
What am I doing wrong?
Thank you


RE: 1 == 1 seems to yield false - buran - Jan-07-2019

Nope, if you look at
(Jan-07-2019, 01:08 PM)zatlas1 Wrote: {'_id': '1'}
you should notice that value is actually '1', i.e. it's a string. and you are comparing it with 1 - that's integer.
In other words '1' == 1 is really False


RE: 1 == 1 seems to yield false - zatlas1 - Jan-07-2019

Thank you so much
I am a newbie in Python; in Perl it would work... I have to eradicate the Perl maven in me :)


RE: 1 == 1 seems to yield false - Gribouillis - Jan-07-2019

zatlas1 Wrote:I have to eradicate the Perl maven in me :)
Yes eradicate Perl and start writing clear code.

If it returns False, it means that you're not testing 1 == 1. Python is robust as stone.