-4

I have a situation where

if len(a) = 0
   a = ''
if len(b) = 0
   b = ''
if len(c) = 0
  c = ''

If len( any object) is zero , declare it as null. I have to keep doing this for all the alphabets . What is the best way to code it.

Additional information :

import test2
x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] # a b c are declared in test2 file as a = set()...b = set() ...
for y, z in enumerate(x):
    if len(getattr(test2, x[y])) >= 5:
            setattr(test2, x[y], '') = ''
    else:
        pass

Why am I not able to use setattr there?

Hyder Tom
  • 373
  • 4
  • 14
  • 1
    If the length is 0 and it's a string, you already have the empty string. – Moses Koledoye Aug 01 '17 at 20:46
  • "If len( any object) is zero , declare it as null" - this isn't declaring anything as null. What do you even think "declare it as null" means? – user2357112 Aug 01 '17 at 20:46
  • 4
    Why do you have 26 single letter variables *to start with*. Use a dictionary or list instead. – Martijn Pieters Aug 01 '17 at 20:47
  • They are of type set() and when the set is empty I need it to declare them as null so that it doesnt print set() . – Hyder Tom Aug 01 '17 at 20:54
  • 1
    You don't need to "declare them as null". Wherever you're printing these sets, if they're empty, *don't print them*. (Also, seriously, don't use 26 single-letter variables.) – user2357112 Aug 01 '17 at 20:57
  • A classic [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Post more of your code and what you are trying to accomplish. – TemporalWolf Aug 01 '17 at 20:59
  • import test2 x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] # a b c are declared in test2 file as a = set()...b = set() ... for y, z in enumerate(x): if len(getattr(test2, x[y])) == 0: setattr(test2, x[y], '') = '' – Hyder Tom Aug 01 '17 at 21:20

1 Answers1

0

The below code exemplifies some horrible programming practices, but is my attempt at trying to understand your request (26 single-letter variables, really?).

a = "fshdj"
b = ""
c = "fshdj"
d = "fshdj"
e = ""
f = "fshdj"
g = "fshdj"
h = "fshdj"
i = "dj"
j = "fshdj"
k = "fshdj"
j = "fsj"
l = "fshdj"
m = "fshdj"
n = "fshdj"
o = "f"
p = "fshdj"
q = "fshdj"
r = " "
s = "fshdj"
t = "fshdj"
u = ""
v = "fshdj"
w = "fshdj"
x = "fshdj"
y = ""
z = "fshdjfshdj"

alpha = [a , b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]

for i in alpha:
  if len(i) == 0:
    i = None
  print i

Try it here!

Crazed
  • 77
  • 12
  • Why does your test2 file define the letters are variables, but they are treated as string by your list in the provided code? – Crazed Aug 01 '17 at 21:34
  • a -z are in a different file where I will have to import them. Thats the reason I was using getattr and setattr. i am not sure how to use if the variables are in other file. Please advice – Hyder Tom Aug 01 '17 at 22:03
  • What's the context of your problem? Is this some sort of homework assignment? If so, please update your post with all the information you have, regardless of what you think is useful. Right now, we don't know what you're trying to accomplish and what you have to do so. – Crazed Aug 01 '17 at 22:05
  • Do you know what `getattr` and `setattr` do? – Crazed Aug 01 '17 at 22:06
  • I have an file which has all the data( Records of members). I will need to read the data files and assign values to variables. Once the record is done i will have to write the record to csv and move to next record( Records is . not standard and the number of variables keep changing. In this process I will have to keep all the variables in one file so that I can write them into csv later on. Once I right to csv will clear the variables and reuse them for other record. – Hyder Tom Aug 01 '17 at 22:25
  • I am assuming getattr is to get the value of variable which is declared else where. So is setattr to set variables which are remotely declared. Please correct me if I am wrong – Hyder Tom Aug 01 '17 at 22:26
  • `setattr` and `getattr` are used for classes, not remotely accessed files. See [this](https://stackoverflow.com/questions/14573021/python-using-variables-from-another-file) for variable access. – Crazed Aug 01 '17 at 22:29
  • As mentioned in code. Test2 has my variables declared. I will have to update those variables ( a, b, c, ....) with values. which i did. now I will have to check if the length of values is greater than 5, if so then make it null else dont touch the variable. – Hyder Tom Aug 01 '17 at 22:44