Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Wayne Bishop
Wayne Bishop
1,572 Points

Variable initialization in Python?

Hi Folks;

Just wondering about object initialization, parameters and return values in Python. Are objects not declared like with Java, C# or Swift? For example:

//declare an integer var someInt: Int = 5

//return a string value func someFunction(param: String) -> String { return "Testing" }

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

No type declaration is needed. The object becomes the type it is assigned, be it an integer, string, some_class_object.

Two things to keep in mind:

  • Everything is an object
  • Parameters are pass by reference. There is a difference between:
    • new_dict = old_dict
    • new_dict = old_dict.copy()
    • new_dict = deepcopy(old_dict)

You can use a type() statement to see what an object is. You might also find dir() useful to see an object's attributes.

Wayne Bishop
Wayne Bishop
1,572 Points

Thanks for the great response! This clears up number of questions as I learn the language.

Regards;