#๐Ÿ”’ ๐Ÿ”’ confused on what i should do when calling a function from a class

78 messages ยท Page 1 of 1 (latest)

blazing walrus
#

it wants a positional argument but idk what to use..

raw pendantBOT
#

@blazing walrus

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

blazing walrus
#

!pastebin

#

help plz

#

no one?

untold gyro
#

are you familiar with the concept of instantiating a class?

blazing walrus
#

nope

blazing walrus
#

!instantiating

#

dang..

bleak lotus
#

methods that have a 'self' parameter are only called on instances of a class, not the class itself

blazing walrus
#

what would i change App.search() to then?

#

i need the search function to run again

#

its inside the class 'App'

#

i need to call it from outside the class

untold gyro
# blazing walrus instantiating?

instantiating means to create an instance of a class. in python, you can create an instance by calling a class similar to a function

#
app = App()
app.search()
blazing walrus
#

huh.. i do that with my watchdog module code

#

event_handler = MoverHandler()

#

line 682

untold gyro
#

yeah, exactly. you need to do that here as well

blazing walrus
#

that would be instantiating right?

untold gyro
#

yes

#

have you ever seen the __init__ function in python?

blazing walrus
#

but i already do that in my bottom if statement

#
if __name__ == "__main__":
    #WATCHDOG STUFF
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    path = source_dir
    event_handler = MoverHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()

    app = App()
    app.mainloop()

bleak lotus
#

which is a global variable, so i suppose you already have access to it

blazing walrus
#

yeah i aint got a clue what to do now..

untold gyro
#

so you already have app = App(). how can you access or reuse the app variable inside of your function?

blazing walrus
#

which function? the one im trying to call the function i want from?

#

so from def move_file(dest, entry, name):

untold gyro
#

yeah, so instead of App.search you want app.search

#

where App is your class, and app is an instance of your class

#

how can you access the app variable you've already declared from inside move_file

blazing walrus
#

then how would it know where to get the function from?

bleak lotus
#

it's an odd design choice for this function move_file to expect a global variable from __name__=='__main__' to be defined, this is spaghetti code, but it'll work...

untold gyro
#

are you familiar with dictionaries?

blazing walrus
blazing walrus
bleak lotus
#

we probably all are, and believe me i've made a lot of nice spaghetti

untold gyro
blazing walrus
#

omg it works..

#

thank you both..

untold gyro
#

oh, what change did you make

blazing walrus
#

lowercasing the App ;-;

#

lol

#

all i did was App.search() -- app.search()

untold gyro
#

ah, well. i suppose in your main if block you're creating a global variable anyway

blazing walrus
#

idk if i should go on self teaching..

untold gyro
#

there's nothing wrong with self teaching

blazing walrus
#

its a fun hobby but hella sloppy and unprofessional

untold gyro
#

that's fine

blazing walrus
#

thank you so much you just made it possible to finish off my second ever app

untold gyro
#

a class won't really teach you any more than you can learn on your own

blazing walrus
#

so i gotta in some way

untold gyro
untold gyro
blazing walrus
#

i got gcse grade 4 in computer science

blazing walrus
#

looks hella messy

#

wth

untold gyro
#

what looks messy?

blazing walrus
#

just his titles

#

raymond hettinger?

#

who he?

untold gyro
#

he is a core contributor to the python programming language

blazing walrus
#

damn thats huge actually.. he updates python?

untold gyro
#

yes

blazing walrus
#

i heard ai might be huge with python

#

its good for algorithms i believe so it looks like it

raw pendantBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ confused on what i should do when calling a function from a class

#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.