0

I'm trying to do something like this. One can import something like: from random import randint and then later assign it like: x = randint()

and what I'm trying to do is try to find a way to dynamically set any value: var x = myint

and use that VALUE to assign to the class instead... like (the value of x) = randint()

so if you printed: print(type(myint))

you'd see: >>> (class random.randint)

Not sure if I'm making any sense but...in php, one just has to do something like @x to make it use the value of the variable and not just reassign the variable. So it would end up like:

x= "myvar"
@x = randint()
print(type(myvar))
>>> (class random.randint)

and then be able to do whatever you wanted later like:

new_int = myvar.randint(0, 50)

Something like that...is what I'm shooting for even possible?

Luxgeek
  • 11
  • 3
  • What does `@` do in PHP other than suppressing error messages??? Not sure what your purpose is, but you can simply do `myvar = randint` and then `myvar(0, 50)` to generate a random integer. Also `randint` is a method, not a class, so it's not possible to see `class random.randint` as your output. – Selcuk Nov 11 '21 at 03:04
  • Don't. Don't do that. It's a terrible practice. There is *never a good reason to do this*. I would just abandon everything you learned in PHP if you are writing Python. – juanpa.arrivillaga Nov 11 '21 at 03:17
  • I actually put the functionality inside a def statement and got it working...I apologize for not coming back here to mark as solved. As for the @ in php...it transfers the VALUE of the variable to the assignment...i.e. myvar = 123 makes the variable myvar = 123...whereas myvar= "test", and then @myvar = 123 instead of myvar having the value of 123, now it would be test = 123. But the way I did it in my python code worked perfectly and I TOTALLY respected the "python way" :) – Luxgeek Nov 12 '21 at 04:33

0 Answers0