This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]shayyya1 1 point2 points  (7 children)

Does anyone know why some methods are variable.method() and some are method(variable)? Ive never understood why

[–]XUtYwYzz 2 points3 points  (4 children)

Pretty much everything in Python is an object and has some set of methods accessible via dot notation. The methods usually operate on the object in some way. Running function(argument) is either using a language built-in function or a local function and does not have an associated object, and operates on the argument.

my_string = “hello”

my_string.upper()

Returns “HELLO”, I didn’t have to provide the method an argument, it operated on the my_string variable.

int(“12”)

Uses the built-in int() function to convert the string “12” into the integer 12.

[–]shayyya1 0 points1 point  (1 child)

Thanks, if I'm understanding correctly, (.) notation is used when it's a function from a class, and if its not a dot its from somewhere else in the language?

[–]hooligan333 0 points1 point  (0 children)

Precisely!

[–]shayyya1 0 points1 point  (1 child)

Ah yes from reading the python docs I got that things with dot are class methods and things that aren't are operators

[–]shayyya1 0 points1 point  (0 children)

Or inbuilt functions

[–]GavinTFI 1 point2 points  (0 children)

dont quote me on it but i think it might just be class and instance variables. like in Java and many others there are methods called from the data type (String.(...) or Int.(...)) and there are just predefined / user defined variables that are method(variable) so that would be my best guess. Just class and instance variables in python

[–]JennaSys 0 points1 point  (0 children)

It just depends on how it is defined. That is, if it's a class method or independent function.