0

This may be a newb question, but I'm a bit of newb with python, so here's what I'm trying to do...

I am using python2.7

I would like to assign a file path as a string into a dict in functionA, and then call this dict in functionB.

I looked at C-like structures in Python to try and use structs with no luck, possibly from a lack of understanding... The below sample is an excerpt from the link.

I also took a look at What are metaclasses in Python?, but I'm not sure if I understand metaclasses either.

So, how would I call assigned parameters in functionaA, within frunctionB such as:

class cstruct:
    path1 = ""
    path2 = ""
    path3 = ""


def functionA():
   path_to_a_file1 = os.path.join("/some/path/", "filename1.txt")
   path_to_a_file2 = os.path.join("/some/path/", "filename2.txt")
   path_to_a_file3 = os.path.join("/some/path/", "filename3.txt")

   obj = cstruct()
   obj.path1 = path_to_a_file1
   obj.path2 = path_to_a_file2
   obj.path3 = path_to_a_file3

   print("testing string here: ", obj.path1)
      # returns the path correctly here



# this is where things fall apart and the print doesn't return the string that I've tested with print(type(obj.path))
def functionB():
   obj = cstructs()
   print(obj.path1)
   print(obj.path2)
   print(obj.path3)

   print(type(obj.path))
      # returns <type 'str'>, which is what i want, but no path


Am I passing the parameters properly for the paths? If not, could someone please let me know what would be the right way to pass the string to be consumed?

Thanks!

  • Why not just use a dict: `obj['path1'] = path_to_a_file1` ? – Arthur Tacca Apr 19 '20 at 21:53
  • 1
    Python doesn't have structs, instead, you would just use a class. Note, your class isn't defined properly. You have class-variables, and you want instance variables, and you probably want to define an appropriate constructor. Don't worry about metaclasses until you fully grasp the basics, you will almost never need a metaclass. – juanpa.arrivillaga Apr 19 '20 at 22:00
  • @ArthurTacca a class is a perfectly reasonable thing to use here. Indeed, `dict` objects really shouldn't be used this way, they are mapping types, but the OP wants a sort of record type. – juanpa.arrivillaga Apr 19 '20 at 22:01
  • 1
    Anyway, you create a new, empty `cstructs` objects: `obj = cstructs()` which is why you are seeing the behavior you are seeing. Again, you really should read up on the basics of classes/instances in Python, it is a core feature of the language. As an aside, you **really** should be using Python 3. – juanpa.arrivillaga Apr 19 '20 at 22:03
  • I tried replacing the the `obj.path1 = path_to_a_file1` for `obj['path1'] = path_to_a_file1` and received an error: `cstruct instance has not attribute '__setitem__'`. Was this correct? – newb art online Apr 19 '20 at 22:05
  • @newbartonline that isn't what the poster is referring to, they are suggesting you use a `dict` object, but using a custom class is actually a much better practice to begin with. You just need to understand how to actually use them. Note, **you aren't actually passing any parameters to `functionB`, and you didn't define it to take any parameters** – juanpa.arrivillaga Apr 19 '20 at 22:06

1 Answers1

0

You need to do something like this:

class Paths:
    def __init__(self, path1, path2, path3):
        self.path1 = path1
        self.path2 = path2
        self.path3 = path3

def functionA():
    path_to_a_file1 = os.path.join("/some/path/", "filename1.txt")
    path_to_a_file2 = os.path.join("/some/path/", "filename2.txt")
    path_to_a_file3 = os.path.join("/some/path/", "filename3.txt")

    obj = Paths(path_to_a_file1, path_to_a_file2, path_to_a_file3)
    return obj

def functionB(paths): # should take a parameter
   # obj = cstructs() don't do this! This would create a *new empty object*
   print(paths.path1)
   print(paths.path2)
   print(paths.path3)

   print(type(paths.path))


paths = functionA()
functionB(paths) # pass the argument

In any case, you really should take the time to read the official tutorial on classes. And you really should be using Python 3, Python 2 is passed its end of life.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • thank you... i would try to use 3.x, but stuck with 2.7 for this. :) – newb art online Apr 19 '20 at 22:21
  • @juarnpa.arrivillaga... thanks again! i went with just init a `list = [None]*3]` at the top of the program and filling the data in `functionaA`, and later calling in `functionB`... i really didn't want to assign data types into the functions A and B, since they have types for other purpose, which is why i was going with the struct route (i should have been explicit in my write up, sorry)... your info helped a lot though, thanks. :) – newb art online Apr 19 '20 at 23:51