Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 1 == 1 seems to yield false
#1
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
Quote
#2
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
Quote
#3
Thank you so much
I am a newbie in Python; in Perl it would work... I have to eradicate the Perl maven in me :)
Quote
#4
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 315 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  Problem about yield, please help!! cls0724 5 245 Apr-08-2020, 05:37 PM
Last Post: deanhystad
  does yield support variable args? Skaperen 0 149 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 826 Jun-04-2019, 10:26 PM
Last Post: snippsat
  yield help chakox 5 794 Apr-13-2019, 09:42 PM
Last Post: chakox
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 585 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  Yield Keyword 6pathsMadara 6 1,006 Aug-24-2018, 11:43 AM
Last Post: 6pathsMadara
  what does "yield '\n'" do? 2sky 2 1,405 Jan-30-2018, 09:35 AM
Last Post: 2sky

Forum Jump:


Users browsing this thread: 1 Guest(s)