Python Forum
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
Reply
#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
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
#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 :)
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  yield usage as statement or expression akbarza 5 752 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Yield generator weird output Vidar567 8 3,198 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Trying to access next element using Generator(yield) in a Class omm 2 1,935 Oct-19-2020, 03:36 PM
Last Post: omm
  Yield statement question DPaul 6 2,449 Sep-26-2020, 05:18 PM
Last Post: DPaul
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,187 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  Problem about yield, please help!! cls0724 5 2,816 Apr-08-2020, 05:37 PM
Last Post: deanhystad
  does yield support variable args? Skaperen 0 1,644 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 4,109 Jun-04-2019, 10:26 PM
Last Post: snippsat
  yield help chakox 5 3,209 Apr-13-2019, 09:42 PM
Last Post: chakox

Forum Jump:

User Panel Messages

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