0

I am new to python programming. I have written sample code as flows.

temp.py

p = 'Tushar'
print(p)

class Basics:

    def __init__(self, name, phNum):
        self.name = name
        self.phNum = phNum

    def getString(self):
        temp = self.name+' '+str(self.phNum)
        print(type(temp))
        return temp

bs = Basics("tushar", 9620207652)
x = bs.getString()

print(x)

def isBlue(isBlue):
    if(isBlue):
        print('Boolean true')
        x = 'true'
    else:
        print('Boolean false')
        x = 'false'
    return x

tus = isBlue(True)
if(tus != None):
    str = bs.getString().split(' ',1)
    print(str[0])
    print(str[1])

Hello.py

from temp import Basics

class Check:

    def __init__(self):
        print('Check obj')

    def createName(self, firstName, lastName):
        str = firstName + ' ' + lastName
        return str

emp = Check()
completeName = emp.createName('Tushar', 'Banne')
print(completeName)

b = Basics('Tushar', 98765432)
val = b.getString
print("Val is {}".format(val))

I am running Hello.py file and getting the below output.

Tushar

class 'str'

tushar 9620207652

Boolean true

class 'str'

tushar 9620207652

Check obj

Tushar Banne

Val is (bound method Basics.getString of 0x0000024ECCCB5B70

The questions that I have are as follows

  1. Why is the entire temp.py getting executed?

  2. How to execute only getString method.

  3. Why is it that when I use parenthesis after getString, it fails.

  4. Why is the val printing object reference?

  5. Am I following the correct standards of python coding?

glglgl
  • 89,107
  • 13
  • 149
  • 217
Tushar Banne
  • 1,587
  • 4
  • 20
  • 38
  • This is the expected behaviour, whole file needs to be executed as python is a dynamic language and you can change stuff anywhere in the module. If `temp.py` imports another file, that whole file will be executed too and so on. – Vaibhav Vishal Aug 16 '19 at 14:15
  • Tagged duplicate to a question which has several good answers. Also you wanna read [python docs](https://docs.python.org/3/reference/import.html) – Vaibhav Vishal Aug 16 '19 at 14:18

2 Answers2

1

First, check this out What does if __name__ == "__main__": do?

When you are importing a python file, all of the code is executed.

  1. What do you mean fails, what is the error?
  2. val = b.getString means that now val references the method getString, that's why it is printed.

  3. No, read the link above, also, python uses snake_case, not camelCase, so call the method get_string, not getString. (this obviously doesn't changes thte

Ron Serruya
  • 3,988
  • 1
  • 16
  • 26
1

Why is the entire temp.py getting executed?

That's how it works. Importing a module means essentially executing it.

How to execute only getString method.

In order to do so, the code in temp.py has to be changed in a way that it is only executed when the module is run at highest level ("as the __main__ module") instead of imported.

You do that this way:

if __name__ == '__main__':
    p = 'Tushar'
    print(p)

class Basics:

    def __init__(self, name, phNum):
        self.name = name
        self.phNum = phNum

    def getString(self):
        temp = self.name+' '+str(self.phNum)
        print(type(temp))
        return temp

if __name__ == '__main__':
    bs = Basics("tushar", 9620207652)
    x = bs.getString()
    print(x)

def isBlue(isBlue):
    if(isBlue):
        print('Boolean true')
        x = 'true'
    else:
        print('Boolean false')
        x = 'false'
    return x

if __name__ == '__main__':
    tus = isBlue(True)
    if(tus != None):
        str = bs.getString().split(' ',1)
        print(str[0])
        print(str[1])

Why is it that when I use parenthesis after getString, it fails.

I don't see it fail in your question.

Why is the val printing object reference?

Because you asked it to. Referring to a method or function means seeing it as an object and printing its string representation. If you call it (with the () behind), you perform a function call.

glglgl
  • 89,107
  • 13
  • 149
  • 217