Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
basic def / if question
#1
Hi everyone,

I'm very new to Python so my questions may be very basic for most of you.

I am not sure I understand the below function. The part I am having trouble understanding is:

reactions = nacho_table.column('Reactions')
    number_wow_reactions = np.count_nonzero(reactions == 'Wow!')
    number_meh_reactions = np.count_nonzero(reactions == 'Meh.')
How does Python know nacho_table is a table and not an array?


def both_or_neither(nacho_table):
    reactions = nacho_table.column('Reactions')
    number_wow_reactions = np.count_nonzero(reactions == 'Wow!')
    number_meh_reactions = np.count_nonzero(reactions == 'Meh.')
    if number_wow_reactions > number_meh_reactions:
        return 'Wow!'
    elif number_wow_reactions < number_meh_reactions:
        return 'Meh.'
    else:
        return 'Okay!'
    # next condition should return 'Meh.'
    ...
    # next condition should return 'Okay!'
    ...

many_nachos = Table().with_column('Nachos', np.random.choice(nachos, 250))
many_nachos = many_nachos.with_column('Reactions', many_nachos.apply(nacho_reaction, 'Nachos'))
result = both_or_neither(many_nachos)
result
Reply
#2
A table is a class, and as such it has methods (method = function belonging to a class, if you haven't touched OOP part of the story yet, you will understand very well when you get there), such as "column".
If nacho_table wasn't a table, and thus not have "column" method, you would get an error.
This has probably happened to you before, if you tried to do arithmetic operations on "non-compatible" operands, or used some function on a data type, for which it isn't defined.
Reply
#3
Hi J.Crater, thanks for the explanation. I think a lot of what you said went over my head :[ . Let me Google what you posted and I will try to make sense of it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 554 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  [solved] Basic question on list matchiing paul18fr 7 1,856 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  Very basic calculator question BoudewijnFunke 4 1,933 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  basic question isinstance tames 5 2,828 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 2,897 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 2,141 Jan-28-2020, 04:33 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,760 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Basic coding question with Python Than999 3 3,100 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  basic question???????? cemdede 2 2,339 Jan-18-2019, 03:05 AM
Last Post: ntd9395
  basic plotting question Devilish 0 1,891 Dec-27-2018, 10:35 PM
Last Post: Devilish

Forum Jump:

User Panel Messages

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