Python Forum
Variable declaration - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Variable declaration (/thread-32782.html)



Variable declaration - Asraful_Islam - Mar-05-2021

Suppose, I have a set A={'a','b','c'}. I want to declare variables look like N_a, N_b, N_c for every 'a','b','c' in A. Thanks in advance.


RE: Variable declaration - ndc85430 - Mar-05-2021

Why? What are you actually trying to achieve?


RE: Variable declaration - Asraful_Islam - Mar-05-2021

(Mar-05-2021, 04:17 PM)ndc85430 Wrote: Why? What are you actually trying to achieve?
For example, I have a relation looks like- a:- b,c,0.8 (called rule in possibilisitcs answer set programming). I like to transfer this relation into an inequality looks like N_a >= min(N_b,N_c,0.8) where N_a, N_b, N_c are variables. We can do it manually but for 100 or 1000 rules how could do that by machine?


RE: Variable declaration - j.crater - Mar-05-2021

For such a large number of values, better use a data structure, such as a list.

If positions of values have some meaning, then using list with indexing should do it for you.


RE: Variable declaration - deanhystad - Mar-05-2021

Do you want to generate code or make variables? Making variables isn't very useful because it is easier to keep track of collections of variables than trying to figure out a way to get the correct attribute name. It is easy to make programmatically create a variable named 'N_a', but it is difficult for anyone to know that 'N_a' exists, and even harder to make something like N_a >= min(N_b,N_c,0.8).

When you say "We can do it manually", what do you mean? How are you doing this manually? I know of no way to manually make variables unless I am running an interactive interpreter. I know how to write code that makes variables. I even know how to write code that knows how to write code.


RE: Variable declaration - nilamo - Mar-25-2021

(Mar-05-2021, 04:10 PM)Asraful_Islam Wrote: Suppose, I have a set A={'a','b','c'}. I want to declare variables look like N_a, N_b, N_c for every 'a','b','c' in A. Thanks in advance.

After you define them, how are you going to use them?